实体框架6:将子对象添加到父列表中,并将子导航属性设置为父 [英] Entity Framework 6: Adding child object to parent's list vs. setting child's navigation property to parent

查看:141
本文介绍了实体框架6:将子对象添加到父列表中,并将子导航属性设置为父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的数据库,其中有两个表MailServersMailDomains. MailDomains的外键列MailServerId指向MailServers中的Id主键列.所以我们这里有一对多的关系.

I have an existing database with two tables MailServers and MailDomains in it. MailDomains has the foreign key column MailServerId pointing to the Id primary key column in MailServers. So we have a one-to-many-relationship here.

我遵循了这篇文章,并通过实体数据模型向导中的从数据库优先编码"模型.这产生了以下两个C#类:

I followed this article and created my Entity Framework POCOs via the "Code first from database" model in the Entity Data Model Wizard. This produced the following two C# classes:

public partial class MailServer
{
    public MailServer()
    {
        MailDomains = new HashSet<MailDomain>();
    }

    public int Id { get; set; }

    public virtual ICollection<MailDomain> MailDomains { get; set; }
}



public partial class MailDomain
{
    public MailDomain()
    {
    }

    public int Id { get; set; }

    public string DomainName { get; set; }

    public int MailServerId { get; set; }
    public virtual MailServer MailServer { get; set; }
}

现在我的问题是,以下两种在数据库中创建和插入新对象的方法之间是否有区别.

Now my question is whether there is any difference between the following two approaches of creating and inserting new objects to the database.

方法(A):将新的孩子添加到父母的列表中:

Approach (A): Adding new child to the parent's list:

        var mailServer = new MailServer();
        var mailDomain = new MailDomain() {
            DomainName = "foobar.net",
        };
        mailServer.MailDomains.Add(mailDomain);

        using(var context = new MyContext){
            context.MailServers.Add(mailServer);
            context.SaveChanges();
        }

方法(B):将孩子的导航属性设置为父对象:

Approach (B): Setting the child's navigation property to the parent:

        var mailServer = new MailServer();
        var mailDomain = new MailDomain() {
            DomainName = "foobar.net",
            MailServer = mailServer,
        };

        using(var context = new MyContext){
            context.MailDomains.Add(mailDomain);
            context.SaveChanges();
        }

我还假定在方法(A)中,新的MailDomain实例自动添加到集合context.MailDomains中,而在方法(B)中,新的MailServer实例自动添加到集合context.MailServers中.是正确的还是我必须手动执行?

I also assume that in approach (A) the new MailDomain instance is automatically added to the collection context.MailDomains while in approach (B) the new MailServer instance is automatically added to the collection context.MailServers. Is that correct or do I have to do that manually?

同样,我的问题是:两种方法可以互换吗? 只是让我感到困惑,在数据库中只能设置一个属性/列(即MailDomains中的外键),而在C#代码中有两个可以修改的属性(每个类中的一个).

So again, my question is: are the two approaches interchangeable? It just confuses me that in the database there is only one property/column to set (namely the foreign key in MailDomains) while in the C# code there are two properties (one in each class) that could be modified.

推荐答案

是的,这两种方法是可以互换的.这使您可以从MailServer或MailDomain的角度创建对象图并将其保存到数据库.

Yes, the two approaches are interchangeable. This allows you to create and save your object graph to the database from either the perspective of the MailServer or the MailDomain.

如果您先执行代码,则可以选择删除不需要的属性和映射.

If you do code-first, you have the option of removing the properties and mappings if they're not needed.

我还假设在方法(A)中,新的MailDomain实例为 在方法(B)中自动添加到context.MailDomains 新的MailServer实例将自动添加到context.MailServers. 那是正确的还是我必须手动进行?

I also assume that in approach (A) the new MailDomain instance is automatically added to context.MailDomains while in approach (B) the new MailServer instance is automatically added to context.MailServers. Is that correct or do I have to do that manually?

这取决于您添加到上下文"的含义.如果您的意思是:持久化后是否自动将其保存到数据库中,答案是肯定的.使用像EF这样的ORM的最大好处之一是,它可以自动保存完整的对象图(并同步PK/FK关系等).

It depends what you mean by "added to the context". If you mean: does it automatically get saved to the database when you persist, the answer is yes. One of the big benefits to using an ORM like EF is that it handles saving a full object graph automatically (and syncing PK/FK relations, etc.).

如果您的意思是:实体在保存前是否可以通过上下文使用,我认为不是(我不确定100%).

If you mean: will the entity be available via the context before saving, I don't think so (I'm not 100% sure).

这篇关于实体框架6:将子对象添加到父列表中,并将子导航属性设置为父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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