C# 用于插入子实体的实体框架主细节插入记录代码的正确方式

C# 用于插入子实体的实体框架主细节插入记录代码的正确方式,c#,.net,entity-framework,C#,.net,Entity Framework,我是实体框架的新手,我想学习更新数据的正确方法 顾客 public partial class Customer { public Customer() { this.Contacts = new ObservableListSource<Contact>(); } public int CustomerId { get; set; } public int CustomerCustomId { get; set; }

我是实体框架的新手,我想学习更新数据的正确方法

顾客

public partial class Customer
{
    public Customer()
    {
        this.Contacts = new ObservableListSource<Contact>();
    }

    public int CustomerId { get; set; }
    public int CustomerCustomId { get; set; }
    public string CustomerName { get; set; }

    public virtual ObservableListSource<Contact> Contacts { get; set; }
}
表格1

如何添加联系人详细信息

如何从datagridView列添加get值

这段代码行吗,或者有人能提出更好的方法吗

在何处/如何将表单中的插入代码删除到单独的类中

谢谢。

var customer=新客户{CustomerName=“test”};
    var customer = new Customer { CustomerName = "test" };
    _context.Customers.Add(customer);
    var newContact = new Contact();   

    customer.Contacts = new ObservableListSource<Contact>();// it needs becorse your collection is null 


    customer.Contacts.Add(newContact);  //just add new child to collection of parent
    _context.Customers.Add(customer);
    _context.SaveChanges();
_context.Customers.Add(客户); var newContact=newContact(); customer.Contacts=newobserveListSource();//它需要,因为您的集合为空 customer.Contacts.Add(newContact)//只需将新的子对象添加到父对象集合中 _context.Customers.Add(客户); _SaveChanges();
更好的方法是在实体对象内部进行集合初始化

public partial class Customer
{
public Customer()
{
    this.Contacts = new ObservableListSource<Contact>();
}

public int CustomerId { get; set; }
public int CustomerCustomId { get; set; }
public string CustomerName { get; set; }
protected ObservableListSource<Contact> _Contacts;
public virtual ObservableListSource<Contact> Contacts 
{ 
  get{            

    if( _Contacts==null) _Contacts= new ObservableListSource<Contact>();            
      return _Contacts;
      } 
    set{
         _Contacts=value;
       } 
     }
} 
公共部分类客户
{
公众客户()
{
this.Contacts=newobserveListSource();
}
public int CustomerId{get;set;}
public int CustomerCustomId{get;set;}
公共字符串CustomerName{get;set;}
受保护的可观测源触点;
公共虚拟资源联系人
{ 
获取{
如果(_Contacts==null)_Contacts=newobserveListSource();
返回(u)联系人;;
} 
设置{
_接触=价值;
} 
}
} 

对不起,这是我的错。我通常使用可观察收集。这是一种标准的练习,你可以像在课堂上那样写那种类型的字。我只是想说明,当您向集合添加内容时,它需要确保集合不为null。EntityFramework从db加载数据并设置整个集合时不会出现问题。但是,如果您试图在代码中的新(刚创建的)对象中插入新值,这将成为一个问题。谢谢您的接受。如果一切正常,请删除您对错误的评论。我认为这对社区来说并不有趣
    var customer = new Customer { CustomerName = "test" };
    _context.Customers.Add(customer);
    var newContact = new Contact();   

    customer.Contacts = new ObservableListSource<Contact>();// it needs becorse your collection is null 


    customer.Contacts.Add(newContact);  //just add new child to collection of parent
    _context.Customers.Add(customer);
    _context.SaveChanges();
public partial class Customer
{
public Customer()
{
    this.Contacts = new ObservableListSource<Contact>();
}

public int CustomerId { get; set; }
public int CustomerCustomId { get; set; }
public string CustomerName { get; set; }
protected ObservableListSource<Contact> _Contacts;
public virtual ObservableListSource<Contact> Contacts 
{ 
  get{            

    if( _Contacts==null) _Contacts= new ObservableListSource<Contact>();            
      return _Contacts;
      } 
    set{
         _Contacts=value;
       } 
     }
}