首页 > 解决方案 > 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 _, ValueTuple 2 parameters) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func 3 verifySucceeded) at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable1 commandBatches, IRelationalConnection connection)在 Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList1 entries) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList1 个条目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”中插入标识列的显式值。

标签: c#entity-frameworkentity-framework-core

解决方案


推荐阅读