使用Z.EntityFramework.Plus进行更新不是在更新数据



我的UnitOfWork存储库中有一个更新方法,如下所示:

public async Task<int> UpdateAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, T>> updateExpression)
{
return await dbSet.Where(predicate).UpdateAsync(updateExpression);
}

我的逻辑中还有一个更新方法,如下所示:其中BetSelection具有作为外键的Selection Id。

private async Task UpdateBetSelections(Table table)
{
await _unitOfWork.BetSelection
.UpdateAsync(bs => bs.Selection.TableId == table.Id && bs.Selection.RoundId == table.CurrentRound,
bs => new BetSelection()
{
Status = bs.Selection.Status
});
}

我正在尝试将一轮的所有BetSelection记录状态更新为其相应Selections状态的状态。查询没有抛出任何错误,也没有更新记录。

更新

根据此,还不支持复杂类型

Github问题

原始答案

不幸的是,看起来有一个bug。我创建了一个小repro,它生成了以下sql。未提及导航属性。

UPDATE A
SET A.[Status] = B.[Status]
FROM [BetSelections] AS A
INNER JOIN ( SELECT [b].[Id], [b].[SelectionId], [b].[Status]
FROM [BetSelections] AS [b]
) AS B ON A.[Id] = B.[Id]

再现的代码

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Z.EntityFramework.Plus;
namespace ConsoleApp1
{
public class Selection
{
public int Id { get; set; }
public int Status { get; set; }
public virtual ICollection<BetSelection> Selections { get; set; }
}
public class BetSelection
{
public int Id { get; set; }
public int Status { get; set; }
public int SelectionId { get; set; }
public virtual Selection Selection { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<Selection> Selections { get; set; }
public DbSet<BetSelection> BetSelections { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=DESKTOP-5PVJ0I5;Database=zefcore-so1;Integrated Security=true");
base.OnConfiguring(optionsBuilder);
}
}
class Program
{
static async Task Main(string[] args)
{
BatchUpdateManager.BatchUpdateBuilder = builder => {
builder.Executing = c =>
{
Console.WriteLine(c.CommandText);
};
};
var db = new AppDbContext();
var selection = db.Selections.Add(new Selection()
{
Status = 1,
});
db.BetSelections.Add(new BetSelection()
{
Selection = selection.Entity,
Status = 2,
});
db.SaveChanges();
await db.BetSelections
.UpdateAsync(x => new BetSelection() { Status = x.Selection.Status });
}
}
}

此外,如果您将父字段重命名为其他字段,在我的情况下为Status111,您将在下面得到sql的无效列异常

UPDATE A
SET A.[Status] = B.[Status111]
FROM [BetSelections] AS A
INNER JOIN ( SELECT [b].[Id], [b].[SelectionId], [b].[Status]
FROM [BetSelections] AS [b]
) AS B ON A.[Id] = B.[Id]

最新更新