[英]Alternatives to using join entities now that EF Core 6.0 DBContext scaffolding no longer maps simple join tables to explicit entity types
I am currently upgrading a large database-first project to EF Core 6.0.我目前正在将一个大型数据库优先项目升级到 EF Core 6.0。 Details of the breaking EF Core 6.0 scaffolding change described in the title can be found here: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/breaking-changes#many-to-many .
可以在此处找到标题中描述的突破性 EF Core 6.0 脚手架更改的详细信息: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/breaking-变化#多对多。 I am also aware of the mitigations available and the open efcore issue to optionally restore join tables.
我也知道可用的缓解措施和开放的 efcore 问题以选择性地恢复连接表。
From the article above:从上面的文章:
It is very rare that the join entity type needs to be used directly when it contains only two foreign keys for the many-to-many relationships.
当连接实体类型只包含多对多关系的两个外键时,很少需要直接使用它。
This is simply not true in the project I am working on.在我正在从事的项目中,这根本不是真的。 My questions are:
我的问题是:
For example, consider a many-to-many relationship between a Person and a Thing.例如,考虑一个人和一个事物之间的多对多关系。 In EF Core 6.0 the join entity is no longer available:
在 EF Core 6.0 中,连接实体不再可用:
public partial class PersonThing
{
public int PersonId { get; set; }
public int ThingId { get; set; }
public virtual Person Person { get; set; }
public virtual Thing Thing { get; set; }
}
And the tables are mapped with a many-to-many relationship instead:而这些表是用多对多关系映射的:
public partial class Person
{
public Person()
{
Thing = new HashSet<Thing>();
}
public int PersonId { get; set; }
public string PersonName { get; set; }
public virtual ICollection<Thing> Thing { get; set; }
}
public partial class Thing
{
public Thing()
{
Person = new HashSet<Person>();
}
public int ThingId { get; set; }
public string ThingName { get; set; }
public virtual ICollection<Person> Person { get; set; }
}
Previously it was possible to update the relationship between existing Persons and Things using ids only:以前可以仅使用 id 更新现有的人和物之间的关系:
public void UpdateThings(IReadOnlyCollection<int> thingIds)
{
// sync PersonThings
var syncQueue = SynchronisationHelper.GetSynchronisationQueue(thingIds, _model.PersonThing, (s, d) => s == d.ThingId);
foreach (var thingId in syncQueue.Inserts)
{
var personThing = new Database.Model.PersonThing
{
PersonId = _model.PersonId,
ThingId = thingId
};
_model.PersonThing.Add(personThing);
}
foreach (var personThing in syncQueue.Deletes)
{
_context.PersonThing.Remove(personThing);
}
}
What is the alternative to this without access to the join entity type?没有访问连接实体类型的替代方法是什么? Must you load the related entity from context just to create the relationship, or is there a better way of achieving this?
您必须从上下文中加载相关实体才能创建关系,还是有更好的方法来实现这一点?
public void UpdateThings(IReadOnlyCollection<int> thingIds)
{
// sync Things
var syncQueue = SynchronisationHelper.GetSynchronisationQueue(thingIds, _model.Thing, (s, d) => s == d.ThingId);
foreach (var thingId in syncQueue.Inserts)
{
var thing = _context.Thing.Single(t => thingId == t.ThingId);
_model.Thing.Add(thing);
}
foreach (var thing in syncQueue.Deletes)
{
_model.Thing.Remove(thing);
}
}
Based on this comment on the open efcore issue to optionally restore join tables, I am satisfied that using join entity types is a perfectly valid database-first approach, and using them (or not) is a question of preference / style.基于对可选恢复连接表的开放式 efcore 问题的评论,我很满意使用连接实体类型是一种完全有效的数据库优先方法,使用(或不使用)它们是偏好/风格的问题。 There are already mitigations available to map join entity types using scaffolding in EF Core 6.0, hopefully the issue is addressed so this can be achieved without a workaround.
map 在 EF Core 6.0 中使用脚手架加入实体类型已经有了缓解措施,希望这个问题得到解决,这样就可以在没有变通办法的情况下实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.