Code First : Unable to create 1 to many relationship. Creates 0...1 to many instead

.net c# ef-code-first entity-framework entity-framework-6

Question

Customer

public class Customer
{
    public Customer()
    {
        Addresses = new List<Address>();
        Reviews = new List<Review>();
        Products = new List<Product>();
    }
    [Key]
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public Address DefaultAddress { get; set; }
    public int DefaultAddressId { get; set; }
    public List<Address> Addresses { get; set; }
    public List<Review> Reviews { get; set; }
    public List<Product> Products { get; set; }
}

Product

public class Product
{
    public Product()
    {
        Reviews = new List < Review >();
    }

    public int Id { get; set; }
    public Category Category { get; set; }
    public string Name { get; set; }  
    public string Description { get; set; }
    public string Specification { get; set; }
    public List<Review> Reviews { get; set; }
    public List<Customer> Customers { get; set; }
}

Review

public class Review
{
    public int Id { get; set; }
    public string Text { get; set; }
    public int Stars { get; set; }
    [Required] 
    public int ProductId { get; set; }
    [Required]
    public string CustomerId { get; set; }
}

}

Generated model

enter image description here

I want the relationship between Review and Customer to 1 to many not 0..1 to many. Each review must belong to one customer. I don't understand how the relationship is mapped properly for Review - Product but not for the customer.

1
0
11/22/2018 8:57:44 AM

Popular Answer

i only used Customer and Review models since those are the once you wanted an answer. Following is the your model classes should be.

public class Customer
{
    public Customer()
    {
        Reviews = new HashSet<Review>();
    }

    [Key]
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }

    public ICollection<Review> Reviews { get; set; }

}

public class Review
{
    public int Id { get; set; }
    public string Text { get; set; }
    public int Stars { get; set; }
    [Required]
    public int ProductId { get; set; }
    [Required]
    public string CustomerId { get; set; }

    [ForeignKey("CustomerId")]
    public Customer Customer { get; set; }
}

this way you can have customers Review collection and cast it in to a list if you want.use Include() method in your linq query to lazy load the customers review collection. for an example:

var customer = dbContext.Customer.Include("Reviews").where(x => x.Email == "john@gmail.com").FirstOrDefault();
0
11/23/2018 8:13:02 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow