实体框架 4.2:使用相同的多对多关系的多个实体

发布于 2025-01-05 23:37:27 字数 2582 浏览 4 评论 0原文

我有一个表Travelers

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

一个表Transporters和一个连接表TransporterTravelers

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

用于在旅行者和运输商之间创建多对多关系。我使用 POCO 类创建实体 Traveler 和 Transporter,并自动创建连接表(使用 CreateDatabaseIfNotExists 初始值设定项)。随着项目的进展,我们关闭了自动数据库创建,因为数据库现在已填充了数据。最近,我们添加了一个视图 vwTravelersSummary 来加快速度,它使用内/左连接处理 Travelers 表和其他几个表:

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 类:

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

我还需要一个多对多-该实体和传输器之间的许多关系(我们正在使用数据服务,并且我们在查询拦截器中需要这种关系)。所以我添加了以下 Fluent API 调用:

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 现在响应错误:

对象名称“dbo.TransporterTravelers1”无效。

(由于基于约定的命名?)。因此,我还明确指定了旅行者和运输者之间的原始多对多关系:

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 必须引用唯一的架构和表。

我该如何解决这个问题? 提前致谢!

I have a table Travelers:

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

a table Transporters and a join table TransporterTravelers

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

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 (...)

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

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

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:

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

Now I get the following error:

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零崎曲识 2025-01-12 23:37:27

你不能。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文