using hashset in entity framework

后端 未结 3 1367
情话喂你
情话喂你 2020-12-13 06:48

I want to know what is the difference between creating classes with or without using \"hashset\" in constructor.

Using code first approach (4.3) one can creat models

3条回答
  •  有刺的猬
    2020-12-13 07:37

    I'm fairly new to Entity Framework but this is my understanding. The collection types can be any type that implements ICollection. In my opinion a HashSet is usually the semantically correct collection type. Most collections should only have one instance of a member (no duplicates) and HashSet best expresses this. I have been writing my classes as shown below and this has worked well so far. Note that the collection is typed as ISet and the setter is private.

    public class Customer
    {
        public Customer()
        {
            BrokerageAccounts = new HashSet();
        }
        public int Id { get; set; }
        public string FirstName { get; set; }
        public ISet BrokerageAccounts { get; private set; }
    }
    

提交回复
热议问题