简体   繁体   中英

Entity Framework Modeling code first

I have what I think is a simple modeling problem, but for some reason I can't wrap my head around what the best way to solve it is.

I have A Person class (That holds information about a user) and I have a PhoneCarrier class that holds information about PhoneCarriers(their name and the email extension to send an email to a persons phone).

This is the Person class

 public class Person
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Display(Name = "First Name")]
    [Required(ErrorMessage ="{0} is required")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    [Required(ErrorMessage ="{0} is required")]
    public string LastName { get; set; }

    [Display(Name = "Dish Order")]
    //[Range(1, byte.MaxValue, ErrorMessage = "Value of {0} must be greater than 0")]
    public  byte DishOrder { get; set; }

    [Display(Name = "E-Mail")]
    [RegularExpression(".+\\@.+\\..+",
    ErrorMessage = "Please enter a valid email address")]
    public string Email { get; set; }

    [DataType(DataType.Text)]
    [RegularExpression(@"(^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$)", ErrorMessage = "{0} must be a numeric value of length 10")]
    [Display(Name = "Phone Number")]
    //[DisplayFormat(DataFormatString = "{0:###-###-####")]
    public string PhoneNumber { get; set; }

    [Display(Name = "Phone Carrier")]
    public int PhoneCarrier { get; set; }

    public virtual ICollection<Fine> Fines { get; set; }

    public virtual ICollection<DishDate> DishDates { get; set; }

    [NotMapped]
    public static IEnumerable<PhoneCarrier> PhoneCarriers { get; set; }

This is the PhoneCarrier class

public class PhoneCarrier
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Required(ErrorMessage ="{0} is required")]
    public string Company { get; set; }

    [Display(Name = "Email Extension")]
    [Required(ErrorMessage ="{0} is required")]
    public string EmailExtension { get; set; }
}

I was wondering if this would be the right way to model this, I feel as if their is a better solution because right now, if I want to get the information about a Person's phone carrier, I have to first get the Person's phonecarrier id, and then I have to call the PhoneCarrier where the id = phonecarrier id. Here is an actual example

var phoneCarrierId = person.PhoneCarrier;
            return db.PhoneCarriers.Where(x => x.ID == phoneCarrierId).FirstOrDefault().EmailExtension;

Is there a way where I could set it up (such as making the int PhoneCarrier field in the Person class of type PhoneCarrier) to where I could just call db.Person.PhoneCarrier.EmailExtension? This seems like a much cleaner approach and I was wondering if that was possible or if the way I'm doing it is the more correct way...

You only need to have a single phone carrier assigned to the person, not the entire list. Also, rename the ID field:

public class Person
{
    //snip

    public int PhoneCarrierId { get; set; }
    public virtual PhoneCarrier PhoneCarrier { get; set; }
}

Now when you get your person, you can do it simply:

var person = db.Persons.Where(p => p.ID == 1);
var carrier = person.PhoneCarrier;

Note: If you are not using lazy loading, then you may need to Include the carrier:

var person = db.Persons.Include(p => p.PhoneCarrier).Where(p => p.ID == 1);

Use navigation properties. See the example here: Code First Conventions . Take a look at how the Department is related to the Course in the examples.

The below code will solve the problem. I recommend changing the id properties in your classes to TableNameId, eg PersonId and PhoneCarrierId.

See here for more info - https://msdn.microsoft.com/en-us/data/jj713564.aspx

public class Person
{
    public int PersonId {get;set;}
    //...
    public int PhoneCarrierId { get; set; }
    public virtual PhoneCarrier PhoneCarrier { get; set; }
}

public class PhoneCarrier 
{
    public int PhoneCarrierId {get;set;}
    //...
}

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