c# - ef核心插入具有导航属性的实体列表
问题描述
我有模型。
public class Lessee
{
public long Id { get; set; }
public string LesseeName { get; set; }
public bool? LogicalDeleteIn { get; set; }
}
和另一个具有导航属性的模型。
public class RevenueReceived
{
public long Id { get; set; }
...
public Lessee Lessee { get; set; }
}
然后我填充承租人列表并插入,效果很好。
public List<Lessee> InsertLessees(List<Lessee> lessees)
{
using (var ctx = new DataContext())
{
ctx.Lessees.AddRange(lessees);
ctx.SaveChanges();
return lessees;
}
}
然后,我创建一个 RevenueReceived 列表并添加相应的承租人。
public List<RevenueReceived> GetRevenueReceived(List<Lessee> lessees)
{
List<RevenueReceived> retval = new List<RevenueReceived>();
using (OleDbConnection con = new OleDbConnection(accessConnectionString))
using (OleDbCommand Command = new OleDbCommand(" SELECT * from tblRevenueReceived", con))
{
con.Open();
OleDbDataReader DB_Reader = Command.ExecuteReader();
while (DB_Reader.Read())
{
string lesseeName = DB_Reader.IsDBNull(DB_Reader.GetOrdinal("Company")) ? null : DB_Reader.GetString(DB_Reader.GetOrdinal("Company"));
retval.Add(new RevenueReceived
{
Id = 0,
...
Lessee = string.IsNullOrEmpty(lesseeName) ? null : lessees.FirstOrDefault(x => x.LesseeName == lesseeName)
});
}
}
return retval;
}
最后我尝试保存收到的收入列表。
public void InsertRevenuesReceived(List<RevenueReceived> revenuesReceived)
{
using (var ctx = new DataContext())
{
ctx.RevenueReceived.AddRange(revenuesReceived);
ctx.SaveChanges();
}
}
我收到以下错误。我有点困惑为什么它试图再次插入到承租人表中。有没有办法只插入主键但不让它再次尝试插入承租人?
Microsoft.EntityFrameworkCore.DbUpdateException HResult=0x80131500
消息=更新条目时出错。有关详细信息,请参阅内部异常。
Source=Microsoft.EntityFrameworkCore.Relational StackTrace: 在 Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection 连接) 在 Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(DbContext _, ValueTuple2 parameters) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func
3 operation, Func3 verifySucceeded) at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable
1 commandBatches, IRelationalConnection connection)在 Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList1 entries) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList
1 个条目ToSave) 在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess) 在 Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess) 在 Microsoft.EntityFrameworkCore.DbContext.SaveChanges() 在 accessdataimport.DataImportService.InsertRevenuesReceived(List` C:\Users\c-bdelling\source\repos\Minerals\accessdataimport\DataImportService.cs 中的 1 个收入):C:\Users\c-bdelling\source 中 accessdataimport.Program.Main(String[] args) 的第 100 行\repos\Minerals\accessdataimport\Program.cs:第 29 行内部异常 1:SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“Lessees”中插入标识列的显式值。
解决方案
推荐阅读
- kubernetes - 如何更改etcd监听的IP和端口
- apache - .htaccess 中的 301 重定向首先在原始 URL 上给出证书警告
- android - 如何从 Observable 打印排放量
>>? - database - `RefreshDatabase` 删除所有表
- vue.js - 为什么我的数据过滤函数返回空?
- python - 一旦收到有效输入,如何打破用户选择的循环?
- c++ - 为一群追逐兔子的狐狸创建植绒算法的最佳方法
- python - go.Histogram2dContour 从 plotly 显示不同的图表类型
- android - InnerClass 的 ClassNotFoundException 因为编译器生成的类路径不同
- odoo-8 - 如何在操作按钮(odoo 8)内同时组合警告消息和更新值