在实体框架中使用hashset [英] using hashset in entity framework

查看:68
本文介绍了在实体框架中使用hashset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在构造函数中使用或不使用hashset创建类之间有什么区别。



使用代码第一种方法(4.3)可以创建模型像这样:

  public class Blog 
{
public int Id {get;组; }
public string标题{get;组; }
public string BloggerName {get; set;}
public virtual ICollection< Post>帖子{get;组;
}

public class Post
{
public int Id {get;组; }
public string标题{get;组; }
public DateTime DateCreated {get;组; }
public string Content {get;组; }
public int BlogId {get;组; }
public ICollection< Comment>评论{get;组;
}

或者可以创建这样的模型:

  public class Customer 
{
public Customer()
{
BrokerageAccounts = new HashSet&BrokerageAccount>() ;
}
public int Id {get;组; }
public string FirstName {get;组; }
public ICollection&BrokerageAccount>经纪人组;
}

public class BrokerageAccount
{

public int Id {get;组; }
public string AccountNumber {get;组; }
public int CustomerId {get;组;

}

什么是hashset在这里做?



我应该在前两个模型中使用hashset吗?



有没有文章显示哈希集的应用?

解决方案

我对Entity Framework相当新颖,但这是我的理解。集合类型可以是实现 ICollection< T> 的任何类型。在我看来,HashSet通常是语义上正确的集合类型。大多数集合只应该有一个成员的实例(不重复),而HashSet最好表达这一点。我一直在写我的课程,如下所示,这到目前为止都很好。请注意,集合键入为 ISet< T> ,setter是私有的。

  public class Customer 
{
public Customer()
{
BrokerageAccounts = new HashSet&BrokerageAccount>();
}
public int Id {get;组; }
public string FirstName {get;组; }
public ISet&BrokerageAccount>经纪人私人集合}
}


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 like this:

public class Blog
 {
     public int Id { get; set; }
     public string Title { get; set; }
     public string BloggerName { get; set;}
     public virtual ICollection<Post> Posts { get; set; }
  }

public class Post
 {
    public int Id { get; set; }
    public string Title { get; set; }
    public DateTime DateCreated { get; set; }
    public string Content { get; set; }
    public int BlogId { get; set; }
    public ICollection<Comment> Comments { get; set; }
 }

or can create models like this :

public class Customer
{
    public Customer()
    {
        BrokerageAccounts = new HashSet<BrokerageAccount>();
    }
    public int Id { get; set; }
    public string FirstName { get; set; }
    public ICollection<BrokerageAccount> BrokerageAccounts { get; set; }
}

public class BrokerageAccount
{

    public int Id { get; set; }
    public string AccountNumber { get; set; }
    public int CustomerId { get; set; }

}

What is hashset doing here?

should i use hashset in the first two models also?

is there any article which shows the application of hashset?

解决方案

I'm fairly new to Entity Framework but this is my understanding. The collection types can be any type that implements ICollection<T>. 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<T> and the setter is private.

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

这篇关于在实体框架中使用hashset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆