0

I have some Models in my app like so:

public class Enquiry
{
    [Key]
    [Required]
    public int EnquiryID { get; set; }

    [Required]
    [Display(Name = "Enquiry Type:")]
    public virtual int EnquiryTypeID { get; set; }
    public virtual EnquiryType EnquiryType { get; set; }

    public virtual ICollection<DeliveryType> DeliveryTypes { get; set; }

}

public class EnquiryType
{
    [Key]
    public int EnquiryTypeID { get; set; }

    [Display(Name = "Enquiry Type:")]
    [MaxLength(100)]
    public string EnquiryTypeName { get; set; }
}

public class DeliveryType
{
    [Key]
    public int DeliveryTypeID { get; set; }
    public int EnquiryID { get; set; }
    public string DeliveryName{ get; set; }
}

So the jist of it is. I have an Enquiry, each Enquiry has a Type of Enquiry (Sales, General, Technical etc..) so this is a One-to-One relationship. Each Enquiry can then have multiple DeliveryTypes attached to it, so its a One-to-Many relationship.

My question is, have I set this up correctly with my models above? Am I missing something? Do I have virtual's in the wrong place/not set up correctly? Do I need EnquiryID to be in my DeliveryType model?

1
  • Do you have any problems with it? Commented May 15, 2012 at 20:14

1 Answer 1

1

You do not need EnquiryID on the DeliveryType model. But, the EnquiryTypeID on the Enquiry should not be virtual. I would set it up like this:

public class Enquiry
{
    [Key]
    [Required]
    public int EnquiryID { get; set; }

    [Required]
    [Display(Name = "Enquiry Type:")]
    public int EnquiryTypeID { get; set; }

    public virtual EnquiryType EnquiryType { get; set; }
    public virtual ICollection<DeliveryType> DeliveryTypes { get; set; }

}

public class EnquiryType
{
    [Key]
    public int EnquiryTypeID { get; set; }

    [Display(Name = "Enquiry Type:")]
    [MaxLength(100)]
    public string EnquiryTypeName { get; set; }
}

public class DeliveryType
{
    [Key]
    public int DeliveryTypeID { get; set; }
    public string DeliveryName{ get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response. Out of curiosity, why would I remove the EnquiryID from DeliveryType. How would the relationship then be set up if DeliveryType has no idea which Enquiry it is attached too?
I guess I'm confused about how you are using DeliveryTypes. Can a single DeliveryType be associated with many Enquiries, or will each DeliveryType only belong to a single Enquiry? If the latter is true, then you are correct - you will need to add EnquiryId to the properties of DeliveryType.
Yeah that latter is true, sorry for the confusion! Thanks for the answer!

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.