实体框架核心多对多更改导航属性名称 [英] Entity Framework Core Many to Many change navigation property names

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

问题描述

我有一个名为"LogBookSystemUsers"的表,并且我想在EF Core 5中设置很多功能.我几乎可以使用它,但是问题是我的ID列名为 SystemUserId LogBookId ,但是当EF执行时加入它会尝试使用 SystemUserID LogBookID .这是我当前的配置代码:

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

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

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