2013-08-02 12 views
1

我一直在努力爲客戶及其聯繫人在MVC 4 EF代碼中首次實現以下邏輯。如何在MVC 4代碼中實現主細節表數據插入首先得到


客戶表

public int ID { get; set; } 

public string Name{ get; set; } 

public string Address { get; set; } 

customer_contact表

public int CustomerID { get; set; } 

public string ContactName { get; set; } 

public string PhoneNo { get; set; } 

public string Email { get; set; } 

在這種情況下一個客戶得到一個以上的聯繫方式與外鍵引用。請任何人指導我做到這一點。感謝和進步。

回答

0

你的設計應該看起來一樣,如果我們在EF考慮一對多的關係

客戶表如下(假設它客戶類)

public int CustomerID { get; set; } 

public string Name{ get; set; } 

public string Address { get; set; } 

public List<CustomerDetail> CustomerDetails{get; set; } 

customer_contact表(假設它爲CustomerDetail類)

public int CustomerDetailId{ get; set; } 

public string ContactName { get; set; } 

public string PhoneNo { get; set; } 

public string Email { get; set; } 

public int CustomerID { get; set; } 

public Customer Customer {get; set; } 
0

您需要添加導航n個屬性,讓你做的主/從插入:

Customer類:

public virtual ICollection<CustomerDetail> CustomerDetails{ get; set; } 

CustomerDetails類:

public virtual Customer Customer { get; set; } 

然後插入一個客戶,詳情:

var customer = new Customer { Name="", Address="" }; 
context.Customers.Add(customer); 

var customerDetails = new CustomerDetails { ContactName="", Customer=customer }; 
context.CustomerDetails.Add(customerDetails); 

context.SaveChanges(); 
相關問題