简体   繁体   English

代码首先添加具有多对多关系的实体

[英]Code first add entity with many-to-many relationship

I have an object Customer that can have multiple customer types and each customer type can have multiple customers. 我有一个对象Customer,可以有多个客户类型,每个客户类型可以有多个客户。 I'm new to EF but have managed to add a customer, but can't seem to get the syntax right for adding the customer's customer types as well. 我是EF的新手,但是设法添加了一个客户,但是似乎也没有正确地添加客户的客户类型的语法。

My customer class (simplified): 我的客户类别(简体):

public partial class Customer
{
        public virtual int Id { get; set;}
        public virtual string Name { get; set;}

    #region Navigation Properties

        public virtual ICollection<CustomerType> CustomerTypes
        { get; set; }

    #endregion
}

Customer type: 客户类型:

public partial class CustomerType
{

        public virtual int Id
        {
            get;
            set;
        }

        public virtual string Name
        {
            get;
            set;
        }

        #region Navigation Properties

        public virtual ICollection<Customer> Customers
        { get; set; }

        #endregion
}

When I run this project a CustomerTypeCustomer table is created with columns Customer_Id and CustomerType_Id so this is fine. 当我运行此项目时,将使用Customer_Id和CustomerType_Id列创建一个CustomerTypeCustomer表,因此很好。

I then create the customer like so: 然后,我像这样创建客户:

// code behind
var customer = new Customer();
customer.name = txtCustomerName.Text;

using (var context = new MyEntities())
{   
    context.Customers.Add(customer);
    context.SaveChanges();  
}

I had a look here Insert/Update Many to Many Entity Framework . 我在这里看到了“ 插入/更新多对多实体框架”。 How do I do it? 我该怎么做? and tried to do something similar with customer types: 并尝试对客户类型执行类似的操作:

var customer = new Customer();
customer.name = txtCustomerName.Text;

// only starting with one customer type selected in a check box list
CustomerType customerType = context.CustomerTypes.FirstOrDefault(i => i.Id == 1);

using (var context = new MyEntities())
{
    // IncidentTypes throws Object reference not set to an instance of an object
    customer.CustomerTypes.add(customerType);

    context.Customers.Add(customer);
    context.SaveChanges();  
}

Am I missing something obvious here? 我在这里错过明显的东西吗?

Thanks in advance. 提前致谢。

Edit: for some reason I only have .Add( no AddToObject, AttachTo etc. 编辑:由于某种原因,我只有.Add(没有AddToObject,AttachTo等。

You must initialize CustomersTypes collection first. 您必须首先初始化CustomersTypes集合。

customer.CustomerTypes = new List<CustomerType>();

You can also add this initialization to your Customer 's constructor. 您还可以将此初始化添加到Customer的构造函数中。

This is one way to do it: 这是一种方法:

var customer = new Customer();
customer.name = txtCustomerName.Text;

// only starting with one customer type selected in a check box list
//add Include there
CustomerType customerType = context.CustomerTypes.Include("Customers").FirstOrDefault(i => i.Id == 1);

using (var context = new MyEntities())
{
    // IncidentTypes throws Object reference not set to an instance of an object
    //customer.CustomerTypes.add(customerType);

    //context.Customers.Add(customer);
    customerType.Customers.Add(customer);
    context.SaveChanges();  
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在实体代码优先中以多对多关系为数据库播种 - How to seed the database with a many-to-many relationship in Entity Code First Entity Framework 4.1 - Code First:多对多关系 - Entity Framework 4.1 - Code First: many-to-many relationship 实体框架,代码优先,如何从多对多关系映射中向表添加主键? - Entity Framework, code-first, how to add primary key to table out of many-to-many relationship mapping? 首先创建多对多关系代码 - Creating many-to-many relationship code first 实体框架-手动向多对多关系添加属性-模型优先 - Entity Framework - Manually Add Properties to Many-to-Many Relationship - Model First 如何在MVC4和实体代码中首先在Fluent中编写多对多关系 - How to write a many-to-many relationship in Fluent in MVC4 and Entity Code First 实体框架代码优先:多对多关系; 从用户中删除一些角色 - Entity Framework Code First: Many-to-Many relationship; Remove some Roles from the User 编写第一个实体框架(EF6)的代码,在初始创建后添加多对多关系 - Code first Entity Framework (EF6) Adding Many-to-Many relationship after initial create 通过具有多对多关系的实体框架代码优先方法为 SQL Server 播种 - Seeding SQL Server by Entity Framework code-first approach with many-to-many relationship Code First Entity Framework中的Seed方法未填充多对多关系中的链接表 - The link table in Many-to-many relationship is not populated by the Seed method in Code First Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM