是否可以在EF Core 3.0中制作表格/实体?基本上,我有要限制插入/更新/删除命令的历史记录表。
我已经通过更改尝试了AsNoTracking:
public DbSet<SampleHistory> SampleHistories { get; set; }
至:
public IQueryable<SampleHistory> SampleHistories { get {return Set<SampleHistory>).AsNoTracking<SampleHistory>(); } }
如果我做这样的事情,它工作正常(什么都没有保存在数据库中):
var v = DbContext.SampleHistories.FirstOrDefault(e => e.HistoryId == 1);
v.Field1 = $"Test{DateTime.Now.ToLongTimeString()}";
DbContext.SaveChanges();
但是,如果我在保存之前添加了更新语句,我会看到数据库中的记录正在更新:
var v = DbContext.SampleHistories.FirstOrDefault(e => e.HistoryId == 1);
v.Field1 = $"Test{DateTime.Now.ToLongTimeString()}";
DbContext.Update(v);
DbContext.SaveChanges();
我也想避免这种情况。
我通过将此类表强制生成为Views来解决了这一要求。
例如:
modelBuilder.Entity<SampleHistory>(eb =>
{
eb.HasNoKey();
eb.ToView("Sample_History");
.
.
.
});
如果您尝试对此类表执行插入,更新或删除操作,则EF会引发无效操作异常。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句