1

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...

1
  • Why do you have IEnumerable<PhoneCarrier> PhoneCarriers in Person. Is there a one-many relationship? Commented Sep 19, 2016 at 13:41

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

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

4 Comments

You have just deleted your answer and made a new one with the same detail, you know that people with 10k rep or more can still see deleted posts?
I don't understand what the problem is, I've made an answer clearer, can't you leave it alone?
If you delete an answer and make another one with the same info, that is bad behaviour. I'm just trying to help you here.
Thanks, I'll keep it mind for the future. Hope you have a good day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.