0

想,我有父母下表結構,如何更新父和子表中的一個交易實體框架

public partial class CustomerDetail 
    { 
     public CustomerDetail() 
     { 
      this.DependentDetails = new HashSet<DependentDetail>(); 

     } 

     public int CustomerId { get; set; } 
     public string Title { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public string Gender { get; set; } 


     public virtual ICollection<DependentDetail> DependentDetails { get; set; } 

    } 

與子表結構,

public partial class DependentDetail 
    { 
     public int DependentId { get; set; } 
     public Nullable<int> CustomerId { get; set; } 
     public string Relationship { get; set; } 
     public string Gender { get; set; } 
     public Nullable<System.DateTime> DateOfBirth { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 

     public virtual CustomerDetail CustomerDetail { get; set; } 
    } 

如果我想使用實體框架更新這兩個表,如何做到這一點?

請建議。

回答

0

插入新記錄使用Entitiy框架

var customer=new CustomerDetail(); 
customer.Title ="Customer"; 
customer.FirstName ="First"; 
customer.LastName ="Last"; 
customer.Gender ="Male"; 
var dependent=new DependentDetail(); 
dependent.Relationship ="Test"; 
. 
. 
. 
customer.DependentDetails.Add(dependent); 

while updating dependent 

var customer=Repository.find(CustomerId); 
//update customer details here 
var dependent=customer.DependentDetails.where(x=>x.DependentId ==DependentId).FirstOrDefault(); 
dependent.RelationShip="Test"; 

///call save function 
+0

我想在一杆,以更新客戶與相關表的數據庫。 – PaRsH

+0

這是什麼調用Repository.save()後,依賴數據設置,以便它將更新客戶和從屬 –