实体框架4.2:使用相同多对多关系的多个实体 [英] Entity Framework 4.2: Multiple entities using the same many-to-many relationships

查看:176
本文介绍了实体框架4.2:使用相同多对多关系的多个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表旅行者

CREATE TABLE [dbo].[Travelers](
    [TravelerId] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [nvarchar](25) NULL,
    [LastName] [nvarchar](50) NULL

转运器和连接表 TransporterTravelers / p>

a table Transporters and a join table TransporterTravelers

CREATE TABLE [dbo].[TransporterTravelers](
    [Transporter_TransporterId] [int] NOT NULL,
    [Traveler_TravelerId] [int] NOT NULL,

在旅行者之间创建多对多关系我使用POCO类创建了Traveler和Transporter,并自动创建了连接表(使用CreateDatabaseIfNotExists initializer),随着项目的进行,我们转向自动创建数据库,因为数据库现在已经被数据填充了。添加了一个视图vwTravelersSummary来加速事物,那个地址使用内/左连接,使用旅行者桌子和其他几个表:

to create a many-to-many relationship between travelers and transporters. I used POCO classes to create the entities Traveler and Transporter, and the join table was automatically created (using CreateDatabaseIfNotExists initializer). As the project progressed we turned of automatic database creation, because the database has now been filled with data. Recently we added a view vwTravelersSummary to speed up things, that addresses the Travelers table and a couple of other tables using inner / left joins:

CREATE view [dbo].[vwTravelersSummary] as
SELECT
    tr.[TravelerId],
    tr.[FirstName],
    tr.[LastName],

    adr.[Street],
    adr.[Number], 
    adr.[PostalCode],
    adr.[Town],
FROM
    [dbo].[Travelers] tr
    LEFT JOIN (...)

我创建了映射到此视图的POCO类:

I've created an POCO-class that is mapped to this view:

[DataServiceKey("TravelerId")]
[MetadataType(typeof(TravelerSummaryMeta))]
[Table("vwTravelersSummary")]
public class TravelerSummary
{
    public TravelerSummary()
    {
    }

    [Key]
    public int TravelerId { get; set; }
    ... 
    public string Street { get; set; }
    public int? Number { get; set; }
    public string PostalCode { get; set; }
    public string Town { get; set; }
}

我还需要这个实体和运输商之间的多对多关系(我们正在使用Data Services,我们在查询拦截器中需要这种关系)。所以我添加了以下Fluent API调用:

I also needed a many-to-many relationship between this entity and transporters (we are using Data Services and we need this relationship in a query interceptor). So I added the following Fluent API call:

modelBuilder.Entity<TravelerSummary>()
    .HasMany(ts => ts.Transporters)
    .WithMany(/* No navigation from Transporter to TravelersSummary */)
    .Map(mc =>
        {
            mc.ToTable("TransporterTravelers");
            mc.MapLeftKey("Traveler_TravelerId");
            mc.MapRightKey("Transporter_TransporterId");
        }
    );

一切似乎都有效,但...旅行者和旅行者之间的原始多对多关系运输商现在变得瘫痪了。 EF现在回应一个错误:

Everything seems to work, but... the original many-to-many relationship between the Travelers and Transporters has now become crippled. EF now responds with an error:


无效的对象名称'dbo.TransporterTravelers1'。

Invalid object name 'dbo.TransporterTravelers1'.

(由于基于会议的命名?)。所以我明确指出了旅行者和运输商之间的原始多对多关系:

(due to convention-based naming?). So I specified explicitly also the original many-to-many relationship between travelers and transporters:

modelBuilder.Entity<Traveler>()
    .HasMany(t => t.Transporters)
    .WithMany(tr => tr.Travelers)
    .Map(mc =>
    {
        mc.ToTable("TransporterTravelers");
        mc.MapLeftKey("Traveler_TravelerId");
        mc.MapRightKey("Transporter_TransporterId");
    }
    );

现在我收到以下错误:


指定的模式无效。错误:
(2219,6):错误0019:EntitySet'TravelerSummaryTransporter'
与模式'dbo'和表'TransporterTravelers'已经
定义。每个EntitySet必须引用唯一的模式和表。

Schema specified is not valid. Errors: (2219,6) : error 0019: The EntitySet 'TravelerSummaryTransporter' with schema 'dbo' and table 'TransporterTravelers' was already defined. Each EntitySet must refer to a unique schema and table.

我该如何解决?
提前感谢!

How can I solve this? Thanks in advance!

推荐答案

你不能。 EF不支持映射表多次,多对多关系由表映射表示。因为你不能使用两种不同的多对多关系(即使它们实际上是一样的,但EF不知道)。

You cannot. EF doesn't support mapping table more than once and many-to-many relationship is represented by table mapping. Because of that you cannot use the table in two different many-to-many relationships (even they are actually the same but EF doesn't know about it).

重用关系的唯一方法是通过继承,但这是在你的模型中没有意义的东西,它可能会导致你另一个问题。

The only way to reuse relationship is through inheritance but that is something which would not make sense in your model and it would probably caused you another issues.

这篇关于实体框架4.2:使用相同多对多关系的多个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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