kcon90 kcon90 - 1 year ago 132
C# Question

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
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")]
ErrorMessage = "Please enter a valid email address")]
public string Email { get; set; }

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

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

This is the PhoneCarrier class

public class PhoneCarrier
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...

Answer Source

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

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