2013-03-03 38 views
1

我有一個與Service模型具有一對一關係的事務模型,然後服務模型有許多ServiceAddons。一對一/一對多

public class Transaction 
{ 
    public int TransactionId {get;set;} 
    public string Username {get;set;} 
    public Service OccasionService {get;set;} 
} 

public class Service 
{ 
    [Key] 
    public int TransactionId {get;set;} 
    public Transaction Transaction { get; set; } 
    public virtual IList<ServiceAddon> ServiceAddons { get; set; } 
} 

public class ServiceAddon 
{ 
    public int ServiceAddonId { get; set; } 
    public int TransactionId { get; set; } 
    public int AddonId { get; set; } 
    public decimal AddonPrice { get; set; } 
    public virtual Addon Addon { get; set; } 
} 

現在軟件包管理器控制檯告訴我,OccasionServiceId不存在ServiceAddon,所以我改變了TRANSACTIONID它,編譯的程序,但現在它說無效的列OccasionServiceId。我該如何解決?

回答

2

您需要指定ForeignKey s,請嘗試類似這樣的操作。我已經添加了對不清楚的位的評論。

public class Transaction 
{ 
    [Key] 
    public int TransactionId {get;set;} 
    public string Username {get;set;} 

    // Whatever the column is 
    public int OccasionServiceId {get;set;} 

    [ForeignKey("OccasionServiceId")] // What on Transaction points to Service's Key 
    public Service OccasionService {get;set;} 
} 

public class Service 
{ 
    // Ideally this would be called ServiceId instead? 
    [Key] 
    public int TransactionId {get;set;} 

    // If this uses TransactionId then are you sure you don't need a Key that is ServiceId 
    public Transaction Transaction { get; set; } 

    [ForeignKey("TransactionId")] // What on ServiceAddon points at Service's Key 
    public virtual ICollection<ServiceAddon> ServiceAddons { get; set; } 
} 

public class ServiceAddon 
{ 
    [Key] 
    public int ServiceAddonId { get; set; } 

    // Don't you need a ServiceId here? 

    public int TransactionId { get; set; } 
    public int AddonId { get; set; } 
    public decimal AddonPrice { get; set; } 
    public virtual Addon Addon { get; set; } 
} 

這裏是我的模型是什麼樣子,如果我做到了:

public class Transaction 
{ 
    [Key] 
    public int TransactionId {get;set;} 
    public string Username {get;set;} 
    public int ServiceId {get;set;} 

    [ForeignKey("ServiceId")] 
    public Service Service {get;set;} 
} 

public class Service 
{ 
    [Key] 
    public int ServiceId {get;set;} 
    public int TransactionId {get;set;} 

    [ForeignKey("TransactionId") 
    public Transaction Transaction { get; set; } 

    [ForeignKey("TransactionId")] // What on ServiceAddon points at Service's Key 
    public virtual ICollection<ServiceAddon> ServiceAddons { get; set; } 
} 

public class ServiceAddon 
{ 
    [Key] 
    public int ServiceAddonId { get; set; } 
    public int ServiceId {get;set;} 

    [ForeignKey("ServiceId")] 
    public Service Service {get;set;} 
} 
+0

謝謝您的回答。我試過並且軟件包管理員說它無法確定服務和事務處理類型之間關聯的主要結束點? – WannaCSharp 2013-03-03 13:17:58

+0

我認爲這可能是因爲你的Service.TransactionId似乎既是外鍵又是主鍵?看到我的第二段代碼。 – 2013-03-03 13:24:44

+0

它應該是這樣的:'Transaction.TransactionId 1 <-> 1 Service.TransactionId'和'Service.ServiceId 1 <-> * ServiceAddon.ServiceId'我用'ServiceAddon'修改了一點答案。祝你好運!睡覺前:P – 2013-03-03 13:29:23