简体   繁体   English

Entity Framework Core 多对多更改导航属性名称

[英]Entity Framework Core Many to Many change navigation property names

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 .我有一个名为“LogBookSystemUsers”的表,我想在 EF Core 5 中设置多对多的功能。我几乎可以让它工作,但问题是我的 ID 列被命名为SystemUserIdLogBookId但是当 EF 进行连接时,它会尝试使用SystemUserIDLogBookID This is my current configuration code:这是我当前的配置代码:

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

I tried this:我试过这个:

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.我不想为多对多表使用 class,因为我在我的项目中全部执行此操作,并且我不想要一堆无用的类。 Any ideas?有任何想法吗?

Interesting bug, consider posting it to EF Core GitHub issue tracker.有趣的错误,请考虑将其发布到 EF Core GitHub 问题跟踪器。

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

And it works for any other FK property names except the {RelatedEntity}Id when related entity PK property is called ID .当相关实体 PK 属性称为ID时,它适用于除{RelatedEntity}Id之外的任何其他 FK 属性名称。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在实体框架核心的多对多关系中获取导航属性时出错 - Error in getting navigation property in a many to many relationship in entity framework core 没有集合导航属性的实体框架一对多 - Entity Framework One to Many with no Collection navigation property 实体框架核心在导航属性中多次插入失败? - Entity Framework core many to many insert in navigation properties failed? Entity Framework Core 在一对多关系中添加具有所需导航属性的新实体? - Entity Framework Core adding new Entity with required navigation property in one-to-many relationship? 实体框架订购(按导航属性)(一对多) - ENTITY FRAMEWORK OrderBy on Navigation Property (one-to-many) 实体框架6没有导航属性的一对多关系 - Entity Framework 6 One-To-Many Relationship without Navigation Property Entity Framework 6在运行时检查导航属性是多对多还是一对多的方法 - Entity Framework 6 way to check at runtime if navigation property is many-to-many or one-to-many 实体框架核心创建多对多关系 - Entity Framework Core Creating a Many to Many Relationship Linq Entity Framework核心中的多对多 - Linq Many to many in Entity Framework core 多对多 Entity Framework Core 如何插入 - Many to many Entity Framework Core how to insert
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM