[英]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 列被命名为
SystemUserId
和LogBookId
但是当 EF 进行连接时,它会尝试使用SystemUserID
和LogBookID
。 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.