映射到很多关系 [英] Mapping many to many relationship

查看:137
本文介绍了映射到很多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用实体框架来处理数据模式中的多对多关系时遇到了麻烦。这是我的模型:

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

这是映射:

    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");
                });
}

我的数据库有一个用户,客户和CustomerUsers表,其列与

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?

谢谢!

我应该注意,我有意试图不从Customer或User类引用CustomerUsers表。大多数时候,CustomerUsers表的有效载荷并不重要,只有哪些客户与哪些用户相关联。有一些报告情况,连接表中的附加信息是必需的,但是由于这不是典型的情况,我想避免通过使用这个附加的间接来混淆模型。

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.

推荐答案

而不是尝试将这个映射到多个对象,将其映射为两个到多个关系。在本教程中,参见

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:

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

这篇关于映射到很多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆