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?

4

1 回答 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; }
}
于 2012-05-15T20:20:49.367 回答