Mapping many to many relationship

元气小坏坏 提交于 2020-01-11 10:27:07

问题


I am have some trouble getting Entity Framework to handle a many to many relationship in my data schema. Here is my model:

public class User
{
    public int UserId { get; set; }
    public int Username { get; set; }
    public IEnumerable<Customer> Customers { get; set; }
    ...
}

public class Customer
{
    public int CustomerId { get; set; }
    ...
}

public class CustomerUser
{
    public int CustomerUserId { get; set; }
    public int CustomerId { get; set; }
    public int UserId { get; set; }
    public DateTime CreatedTimestamp { get; set; }
    ...
}

Here is the mapping:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<User>().HasKey(u => u.UserId).ToTable("Users");
        modelBuilder.Entity<Customer>().HasKey(c => c.CustomerId).ToTable("Customer");
        modelBuilder.Entity<CustomerUsers>().HasKey(cu => cu.CustomerUserId).ToTable("CustomerUsers");

        modelBuilder.Entity<CustomerUsers>()
            .HasRequired(cu => cu.User)
            .WithRequiredDependent()
            .Map(m =>
                {
                    m.ToTable("Users");
                    m.MapKey("CustomerUsers.UserId");
                });
}

My database has a Users, Customers, and CustomerUsers table with columns that match the model.

I am trying to execute the following query:

result = (from u in context.Users
                      join customerUsers in context.CustomerUsers on u.UserId equals customerUsers.User.UserId
                      join customers in context.Customers on customerUsers.CustomerId equals customers.CustomerId into ps
                      select new
                      {
                          User = u,
                          Customers = ps
                      }).ToList().Select(r => { r.User.Customers = r.Customers.ToList(); return r.User; });

When I run the code, I get the following error:

The Column 'CustomerUserId' specified as part of this MSL does not exist in MetadataWorkspace

Can anyone see what is wrong with my approach?

Thanks!

I should note that I am intentionally trying to not include a reference to the CustomerUsers table from either the Customer or User class. The majority of the time, the payload of the CustomerUsers table is not important, only which customers are associated to which users. There are some reporting scenarios where the additional information in the join table is necessary, but since this is not the typical situation, I would like to avoid cluttering up the models by having this additional indirection.


回答1:


Instead of trying to map this as many to many, map it as two one to many relationships. See the discussion of many to many join tables with payload in Many-to-Many Relationships in this tutorial:

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application




回答2:


For your model you will need probably two one-to-many relationships and the following navigation properties:

public class User
{
    public int UserId { get; set; }
    public int Username { get; set; }
    // ...
    public ICollection<CustomerUser> CustomerUsers { get; set; }
}

public class Customer
{
    public int CustomerId { get; set; }
    //...
    public ICollection<CustomerUser> CustomerUsers { get; set; }
}

public class CustomerUser
{
    public int CustomerUserId { get; set; }
    public int CustomerId { get; set; }
    public int UserId { get; set; }
    public DateTime CreatedTimestamp { get; set; }
    //...
    public User User { get; set; }
    public Customer Customer { get; set; }
}

And the following mapping:

modelBuilder.Entity<CustomerUser>()
    .HasRequired(cu => cu.User)
    .WithMany(u => u.CustomerUsers)
    .HasForeignKey(cu => cu.UserId);

modelBuilder.Entity<CustomerUser>()
    .HasRequired(cu => cu.Customer)
    .WithMany(c => c.CustomerUsers)
    .HasForeignKey(cu => cu.CustomerId);


来源:https://stackoverflow.com/questions/9470302/mapping-many-to-many-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!