Entity Framework Core 多对多更改导航属性名称 [英] Entity Framework Core Many to Many change navigation property names

查看:21
本文介绍了Entity Framework Core 多对多更改导航属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为LogBookSystemUsers"的表.并且我想在 EF Core 5 中设置多对多的功能.我几乎让它工作了,但问题是我的 ID 列被命名为 SystemUserIdLogBookId 但是当 EF 执行join 它尝试使用 SystemUserIDLogBookID.这是我当前的配置代码:

I have a table called "LogBookSystemUsers" and I want to setup many to many functionality in EF Core 5. I almost have it working but the problem is my ID columns are named SystemUserId and LogBookId but when EF does the join it tries to use SystemUserID and LogBookID. This is my current configuration code:

modelBuilder.Entity<SystemUser>()
            .HasMany(x => x.LogBooks)
            .WithMany(x => x.SystemUsers)
            .UsingEntity(x =>
            {
                x.ToTable("LogBookSystemUsers", "LogBooks");
            });

我试过了:

modelBuilder.Entity<SystemUser>()
            .HasMany(x => x.LogBooks)
            .WithMany(x => x.SystemUsers)
            .UsingEntity<Dictionary<string, object>>("LogBookSystemUsers",
                x => x.HasOne<LogBook>().WithMany().HasForeignKey("LogBookId"),
                x => x.HasOne<SystemUser>().WithMany().HasForeignKey("SystemUserId"),
                x => x.ToTable("LogBookSystemUsers", "LogBooks"));

但这只是添加了两个新列,而不是设置当前列的名称.

But that just adds two new columns instead of setting the names of the current columns.

首先是所有数据库.我不想为多对多表使用一个类,因为我在我的项目中一直这样做,我不希望一堆无用的类四处飘散.有什么想法吗?

This is all database first. I don't want to have to use a class for the many to many table because I do this all over in my project and I don't want a bunch of useless classes floating around. Any ideas?

推荐答案

有趣的错误,请考虑将其发布到 EF Core GitHub 问题跟踪器.

Interesting bug, consider posting it to EF Core GitHub issue tracker.

根据您所尝试的想法应该可以做到

By idea what you have tried should do it

modelBuilder.Entity<SystemUser>()
    .HasMany(x => x.LogBooks)
    .WithMany(x => x.SystemUsers)
    .UsingEntity<Dictionary<string, object>>("LogBookSystemUsers",
        x => x.HasOne<LogBook>().WithMany().HasForeignKey("LogBookId"),
        x => x.HasOne<SystemUser>().WithMany().HasForeignKey("SystemUserId"),
        x => x.ToTable("LogBookSystemUsers", "LogBooks"));

当相关实体 PK 属性称为 ID 时,它适用于除 {RelatedEntity}Id 之外的任何其他 FK 属性名称.

And it works for any other FK property names except the {RelatedEntity}Id when related entity PK property is called ID.

作为解决方法,直到它得到修复,在配置关系之前,明确定义所需的连接实体属性:

As workaround until it gets fixed, define explicitly the desired join entity properties before configuring the relationship:


// add this
modelBuilder.SharedTypeEntity<Dictionary<string, object>>("LogBookSystemUsers", builder =>

{
    builder.Property<int>("LogBookId");
    builder.Property<int>("SystemUserId");
});
// same as the original
modelBuilder.Entity<SystemUser>()
    .HasMany(x => x.LogBooks)
    .WithMany(x => x.SystemUsers)
    .UsingEntity<Dictionary<string, object>>("LogBookSystemUsers",
        x => x.HasOne<LogBook>().WithMany().HasForeignKey("LogBookId"),
        x => x.HasOne<SystemUser>().WithMany().HasForeignKey("SystemUserId"),
        x => x.ToTable("LogBookSystemUsers", "LogBooks"));

这篇关于Entity Framework Core 多对多更改导航属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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