英文:
ExecuteUpdate update lists of objects efcore .net core
问题
我想知道新的ExecuteUpdate是否可以更新导航属性,这个属性是一个列表?
await _unitOfWork.Repositories<Domain.Team>().ExecuteUpdate(
x => x.Id == request.UpdateTeamMembersDto.Id,
x => x.SetProperty(x => x.TeamMembers, teamMembers)
);
这里团队和员工实体之间存在多对多关系,TeamMembers是中间表,所以我想使用ExecuteUpdate来更新TeamMembers列表,因为我不想在更新之前检索所有相关数据。
在这个上下文中,更新的含义是通过添加或删除新记录到TeamMembers属性来添加/删除TeamMembers到团队。
另一个问题是,ExecuteUpdate不等待SaveContext来保存更改,我可以改变这个行为,以便如果事务中出现问题,表不会受到影响吗?
这个方法相对较新,所以我没有找到类似这种的复杂用例。
英文:
I wonder if the new ExecuteUpdate can update navigation property which is a list?
await _unitOfWork.Repositories<Domain.Team>().ExecuteUpdate(
x => x.Id == request.UpdateTeamMembersDto.Id,
x => x.SetProperty(x => x.TeamMembers, teamMembers)
);
Here there is a many to many relationship between team and employee entities, TeamMembers is the intermidate table, so i want to update the list of TeamMembers using ExecuteUpdate because I didn't want to retrieve all the related data before updating them.
Update in this context mean Add/Remove TeamMembers from the Team by removing or adding new record to the TeamMembers property.
Another question ExecuteUpdate does not wait for SaveContext to save the changes, can i change this behavior so that if something goes wrong in the transaction the tables don't get effected?
This method is relatively new so i didn't found a complex use case for it like this.
答案1
得分: 1
没有,你不能使用ExecuteUpdate
来更新导航属性(关系)。
public class UnitTest1
{
private readonly BloggingContext db;
private readonly Blog blog;
private readonly List<Post> posts;
public UnitTest1()
{
db = new BloggingContext();
db.Database.EnsureCreated();
blog = new Blog() {Url="1"};
posts = new (){ new Post(){Title="c"}, new Post(){Title="d"}};
db.Blogs.Add(blog);
var n = db.SaveChanges();
Assert.Equal(1, n);
}
[Fact]
public void Test1() // <-- as usual
{
blog.Posts.AddRange(posts);
var n = db.SaveChanges();
Assert.Equal(2, n);
}
[Fact]
public void Test2() // <-- bulk
{
var n = db.Blogs
.Where(b => b.BlogId == blog.BlogId)
.ExecuteUpdate(
b => b.SetProperty(b => b.Posts, b => posts));
Assert.Equal(2, n);
}
}
失败! - 失败:1,通过:1,跳过:0,总共:2,持续时间:21毫秒
- 测试1通过。
- 测试2出错:
错误消息:
System.InvalidOperationException:无法翻译LINQ表达式'DbSet<Blog>()
.Where(b => b.BlogId == __blog_BlogId_0)
.ExecuteUpdate(b => b.SetProperty<List<Post>>(
propertyExpression: b => b.Posts,
valueExpression: b => __posts_1))'。附加信息:以下'SetProperty'无法翻译:'SetProperty(b => b.Posts, b => __posts_1)'。在实体类型'Blog'上翻译成员'Posts'失败。这通常发生在指定的成员未映射时。有关更多信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038。
英文:
Is just a test to check it, spoiler:
No, you can not update navigation properties (Relationships) using ExecuteUpdate
.
public class UnitTest1
{
private readonly BloggingContext db;
private readonly Blog blog;
private readonly List<Post> posts;
public UnitTest1()
{
db = new BloggingContext();
db.Database.EnsureCreated();
blog = new Blog() {Url="1"};
posts = new (){ new Post(){Title="c"}, new Post(){Title="d"}};
db.Blogs.Add(blog);
var n = db.SaveChanges();
Assert.Equal(1, n);
}
[Fact]
public void Test1() // <-- as usual
{
blog.Posts.AddRange(posts);
var n = db.SaveChanges();
Assert.Equal(2, n);
}
[Fact]
public void Test2() // <-- bulk
{
var n = db.Blogs
.Where(b => b.BlogId == blog.BlogId)
.ExecuteUpdate(
b => b.SetProperty(b => b.Posts, b => posts));
Assert.Equal(2, n);
}
}
>Failed! - Failed: 1, Passed: 1, Skipped: 0, Total: 2, Duration: 21 ms
- Test 1 passed.
- Test 2 error:
Error Message:
System.InvalidOperationException : The LINQ expression 'DbSet<Blog>()
.Where(b => b.BlogId == __blog_BlogId_0)
.ExecuteUpdate(b => b.SetProperty<List<Post>>(
propertyExpression: b => b.Posts,
valueExpression: b => __posts_1))' could not be translated. Additional information: The following 'SetProperty' failed to translate: 'SetProperty(b => b.Posts, b => __posts_1)'. Translation of member 'Posts' on entity type 'Blog' failed. This commonly occurs when the specified member is unmapped. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论