简体   繁体   中英

EF4.1 Code First Question

I want to create my first application using the EF 4.1 Code First Model. I want to model a magazine subscription but just want to check that my POCO classes are fit for purpose.

The following are my classes. Am I missing anything?

Should Customer be a member of Subscription or should it be just that List be a member of Customer?

Thanks.

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; }
}

If a Customer have X suscriptions, every suscription should have the customerID so you need that in your Suscription class (public int CustomerID {get; set;}).

OTOH, I think that you have to put that Customer reference as virtual and the suscription one (I don't know why).

Maybe Im wrong but that works for me.

Anyway, what's your problem?

Your models look correct. You don't necessarily need the Customer property in the Subscription class, but it does help if you want to retrieve a specific Subscription and then want to find the customer that is tied to that subscription. In that instance you can then do var customer = mySub.Customer instead of querying for a customer with the specific subscription id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM