Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 在实体框架中使用hashset_Entity Framework - Fatal编程技术网

Entity framework 在实体框架中使用hashset

Entity framework 在实体框架中使用hashset,entity-framework,Entity Framework,我想知道在构造函数中使用或不使用“hashset”创建类有什么区别 使用代码优先方法(4.3)可以创建如下模型: public class Blog { public int Id { get; set; } public string Title { get; set; } public string BloggerName { get; set;} public virtual ICollection<Post> Posts { get;

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

使用代码优先方法(4.3)可以创建如下模型:

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; }
 }
公共类博客
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection Posts{get;set;}
}
公营职位
{
公共int Id{get;set;}
公共字符串标题{get;set;}
public DateTime DateCreated{get;set;}
公共字符串内容{get;set;}
public int BlogId{get;set;}
公共ICollection注释{get;set;}
}
或者可以创建如下模型:

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; }

}
公共类客户
{
公众客户()
{
BrokerageAccounts=newHashSet();
}
公共int Id{get;set;}
公共字符串名{get;set;}
公共ICollection BrokerageAccounts{get;set;}
}
公共类经纪公司
{
公共int Id{get;set;}
公共字符串AccountNumber{get;set;}
public int CustomerId{get;set;}
}
hashset在这里做什么

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


有没有介绍hashset应用程序的文章?

我对Entity Framework相当陌生,但这是我的理解。集合类型可以是实现
ICollection
的任何类型。在我看来,哈希集通常是语义正确的集合类型。大多数集合应该只有一个成员实例(没有重复),HashSet最好地表达了这一点。我一直在写我的课程,如下所示,到目前为止效果很好。请注意,集合的类型为
ISet
,setter是私有的

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; }
}
公共类客户
{
公众客户()
{
BrokerageAccounts=newHashSet();
}
公共int Id{get;set;}
公共字符串名{get;set;}
公共ISet BrokerageAccounts{get;私有集;}
}

一般来说,最好使用最能表达您意图的收藏。如果您没有特别打算使用HashSet的独特特性,我不会使用它

它是无序的,不支持按索引查找。此外,它不像其他集合那样适合顺序读取,而且它允许您多次添加同一项而不创建重复项,这一事实只有在您有理由使用它的情况下才有用。如果这不是您的意图,它可能会隐藏行为不端的代码,并使问题难以隔离

哈希集在插入和删除时间非常重要的情况下非常有用,例如在处理数据时。它对于使用诸如intersect、except和union之类的操作来比较数据集(同样在处理时)也非常有用。在任何其他情况下,弊大于利


考虑到在处理博客文章时,插入和删除与读取相比是非常罕见的,而且您通常希望以特定的顺序读取数据。这或多或少与HashSet的擅长正好相反。无论出于何种原因,您是否打算两次添加同一个post都是非常值得怀疑的,我看不出您为什么会在这样的类中对post使用基于集的操作。

HashSet没有定义实际获取数据时将生成的集合类型。这将始终是声明的ICollection类型

在构造函数中创建的HashSet可以帮助您避免在关系的多个端没有获取记录或不存在记录时出现NullReferenceException。这绝对不是必需的

例如,根据你的问题,当你试图使用像

var myCollection=Blog.Posts();
如果不存在帖子,则
myCollection
null
。这没关系,直到你熟练地把事情联系起来,做一些类似的事情

var myCollectionCount=Blog.Posts.Count();
这将导致出现
NullReferenceException
错误

何处为

var myCollection=Customer.BrokerageAccounts();
var myCollectionCount=Customer.BrokerageAccounts.Count();

将导致和空ICollection以及零计数。没有例外:-)

我完全同意。在大多数情况下,
HashSet
是最自然的匹配。HashSet在EF6.x中似乎仍然正确。就本机而言,EF在db首次创建类型时也会以这种方式使用哈希集。属性上的
()
是否有效(
Blog.Posts()
)?不应该只通过
Blog.Posts
访问该字段吗?这似乎是错误的。调试器精确地显示了我在构造函数中使用的类型,即使是从数据库获取的数据。这也反映在访问集合时的不同行为中(例如,通过对这些集合进行数据绑定)。@linac定义返回类型的不是HashSet,而是ICollection属性的定义。HashSet仅用于初始化ICollection属性。如果未在构造函数中初始化属性,调试器仍将显示定义的ICollection类型。与哈希集无关!!您必须将属性标记为虚拟,以便EF覆盖集合类型。否则,它除了保留可用列表之外别无选择。