ASP.NET实体框架6集合的HashSet或List? [英] ASP.NET Entity Framework 6 HashSet or List for a collection?

查看:196
本文介绍了ASP.NET实体框架6集合的HashSet或List?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class ContentStatus 
{
public ContentStatus()
{
this.Contents = new List< Content>();
}

public int ContentStatusId {get;组; }
public string Name {get;组; }
public virtual ICollection< Content>内容{get;组;
}

然而,我也看到了这样的实现:

  public class ContentStatus 
{
public ContentStatus()
{
this.Contents = new HashSet< ;内容>();
}

public int ContentStatusId {get;组; }
public string Name {get;组; }
public virtual ICollection< Content>内容{get;组; }
}

这是对象的DDL:

  CREATE TABLE [dbo]。[ContentStatus](
[ContentStatusId] INT NOT NULL,
[名称] NVARCHAR(50)NOT NULL,
CONSTRAINT [PK_ContentStatus] PRIMARY KEY CLUSTERED([ContentStatusId] ASC)
);

任何人都可以告诉我哪些应该使用,甚至有什么区别,什么时候使用列表而当HashSet适用的时候。



感谢

解决方案

这取决于你的用例,例如,您只能将一个项目添加到集合中一次,因为例如每个状态仅应用于内容一次。我怀疑你可以有一个内容出现两次状态。因此,HashSet是正确的数据结构,因为它将防止重复。如果一个项目可以重复列表将是正确的,但我没有在实践中遇到这个,甚至不知道EF如何处理它。



作为附注我建议您不要在您的实体中包含项目集合,除非您需要。例如,如果您正在构建一个Web应用程序来列出产品,那么您可能会在其中显示单个产品及其标签的视图。因此,产品应该有一个标签的集合,使这种情况变得容易。但是,您可能没有显示标签及其产品集合的页面,因此标签不应具有产品属性。它只是不关心相关产品。似乎这个Status实体并不关心它收集的内容。


My EF models look like this:

public class ContentStatus
{
    public ContentStatus()
    {
        this.Contents = new List<Content>();
    }

    public int ContentStatusId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Content> Contents { get; set; }
}

However I have also seen implementatins looking like this:

public class ContentStatus
{
    public ContentStatus()
    {
        this.Contents = new HashSet<Content>();
    }

    public int ContentStatusId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Content> Contents { get; set; }
}

Here is the DDL for this Object:

CREATE TABLE [dbo].[ContentStatus] (
    [ContentStatusId] INT           NOT NULL,
    [Name]            NVARCHAR (50) NOT NULL,
    CONSTRAINT [PK_ContentStatus] PRIMARY KEY CLUSTERED ([ContentStatusId] ASC)
);

Can anyone tell me which I should use or even is there a difference and when would I use the List and when the HashSet if that applies.

Thanks

解决方案

It depends on your use case but in most cases you can add an item to the collection only once because for example each status is applied only once to a content. I doubt you can have one content appear twice in a status. Therefore HashSet is the correct data structure as it will prevent duplicates. In case where one item can be duplicated List would be correct but I have not encountered this in practice and do not even know how EF would handle it.

As a side note I would advise that you do not include a collection of items in your entities unless you need it. For example if you are building a web app to list products you probably have a view where you display a single product together with its tags. Therefore Product should have a collection of Tags to make this case easy. However you probably do not have a page that displays a Tag with its collection of products and therefore the Tag should not have a Products property. It just doesn't care about related products. It seems that this Status entity does not care about its collection of Contents.

这篇关于ASP.NET实体框架6集合的HashSet或List?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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