2011-04-14 80 views
2

我想使用EF 4.1 Code First Model創建我的第一個應用程序。我想爲雜誌訂閱建模,但只想檢查我的POCO類是否適合用途。EF4.1 Code First Question

以下是我的課程。我錯過了什麼?

客戶應該是訂閱的成員還是應該只是該列表成爲客戶的成員?

謝謝。

public class Subscription 
{ 
    public int SubscriptionID { get; set; } 
    public string CardNumber { get; set; } 
    public DateTime StartDate { get; set; } 
    public DateTime EndDate { get; set; } 
    public decimal NetPrice { get; set; } 
    public decimal Tax { get; set; } 
    public decimal Discount { get; set; } 
    public string PromotionalCode { get; set; } 
    public Customer Customer{ get; set; } 
} 

public class Customer { 
    public int CustomerID { get; set; } 
    public string Title { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public PostalAddress PostalAddress { get; set; } 
    public string EmailAddress { get; set; } 
    public string Telephone { get; set; } 
    public string Mobile { get; set; } 
    public DateTime DateOfBirth { get; set; } 
    public string Username { get; set; } 
    public string Password { get; set; } 
    public string SecurityQuestion { get; set; } 
    public string SecurityAnswer { get; set; } 
    public bool Valid { get; set; } 
    public IList<Subscription> Subscriptions { get; set; } 
    public bool MailMarketing { get; set; } 
    public bool PartnerMailMarketing { get; set; } 
} 

public class PostalAddress 
{ 
    public int ID { get; set; } 
    public string Address1 { get; set; } 
    public string Address2 { get; set; } 
    public string Address3 { get; set; } 
    public string City { get; set; } 
    public string Postcode { get; set; } 
    public string Region { get; set; } 
    public string Country { get; set; } 
} 

回答

1

如果客戶有X個suscriptions,每個suscription都應該有customerID,所以你需要在你的Suscription類中(public int CustomerID {get; set;})。

OTOH,我認爲你必須把那個客戶參考作爲虛擬的,並且懷疑它(我不知道爲什麼)。

也許我錯了,但對我很有用。

無論如何,你有什麼問題?

+0

它只需要虛擬,如果你想懶加載(這可能是他想要的) – KallDrexx 2011-04-14 19:34:32

+0

感謝您對此回覆。在我繼續之前,我只是想確保自己處於正確的位置。 – FloatLeft 2011-04-14 23:45:09

+0

@KallDrexx很高興知道,謝謝。 – 2011-04-15 13:59:03

0

您的模型看起來是正確的。您不一定需要Subscription類中的Customer媒體資源,但如果您想要檢索特定的Subscription,並且希望找到與該訂閱綁定的客戶,那麼它的確具有幫助。在這種情況下,您可以執行var customer = mySub.Customer而不是查詢具有特定訂閱ID的客戶。