在 Entity Framework Core 中评估 List 对象的 EntityState 时出现异常
Exception while evaluating EntityState of a List object in Entity Framework Core
我正在构建一个小型日志记录应用程序。在少数情况下,我通过评估 EntityState
来保存一个 TransactionHistory
对象。 DomainObject
(TransactionHistory
) 添加新对象时,实体状态变为已添加,SaveChanges()
工作正常。
public void LogTransactionHistory()
{
var transactionsHistory = new TransactionsHistory();
transactionsHistory.TransactionId = 1123;
transactionsHistory.Status = 2;
transactionsHistory.Comments = "Single Entity";
context.TransactionsHistory.Add(transactionsHistory);
if (context.Entry(transactionsHistory).State == EntityState.Added)
{
context.SaveChanges();
}
}
并且在保存 TransactionHistory
对象列表并评估 EntityState
时,我得到了这个异常:
The entity type 'List' was not found. Ensure that the entity type has been added to the model
这是我的代码:
public void LogHistoryList()
{
var transactionHistoryList = new List<TransactionsHistory>();
transactionHistoryList.Add(new TransactionsHistory()
{
TransactionId = 50331,
Status = 2,
Comments = "Multiple entities",
});
context.TransactionsHistory.AddRange(transactionHistoryList );
var addedEntities = context.ChangeTracker.Entries()
.Where(x => x.State == EntityState.Added);
// checking the entity state of transactionHistoryList
if (context.Entry(transactionHistoryList).State == EntityState.Added) // exception
{
context.SaveChanges();
}
}
型号:
public partial class TransactionsHistory
{
public long TransactionId { get; set; }
public short? Status { get; set; }
public string Comments {get ;set;}
}
谁能帮我解决这个问题?我错过了什么吗?
提前致谢
在这种情况下,您在 addedEntities
中获得了来自 ChangeTracker
的所有 added
个实体,并且不需要在 if (context.Entry(transactionHistoryList).State == EntityState.Added)
.
中再次检查
context.Entry
接受一个 entity
但你将 entities
的范围传递给 context.Entry
你必须改成这个
context.TransactionsHistory.AddRange(transactionHistoryList);
var addedEntities = context.ChangeTracker.Entries()
.Any(x => x.State == EntityState.Added);
if (addedEntities)
{
context.SaveChanges();
}
更新
如果你想检查所有添加的实体,你可以使用这个
foreach(var entity in ChangeTracker.Entries())
{
if(entity.State = EntityState.Added)
{
//DoSomething
}
}
我正在构建一个小型日志记录应用程序。在少数情况下,我通过评估 EntityState
来保存一个 TransactionHistory
对象。 DomainObject
(TransactionHistory
) 添加新对象时,实体状态变为已添加,SaveChanges()
工作正常。
public void LogTransactionHistory()
{
var transactionsHistory = new TransactionsHistory();
transactionsHistory.TransactionId = 1123;
transactionsHistory.Status = 2;
transactionsHistory.Comments = "Single Entity";
context.TransactionsHistory.Add(transactionsHistory);
if (context.Entry(transactionsHistory).State == EntityState.Added)
{
context.SaveChanges();
}
}
并且在保存 TransactionHistory
对象列表并评估 EntityState
时,我得到了这个异常:
The entity type 'List' was not found. Ensure that the entity type has been added to the model
这是我的代码:
public void LogHistoryList()
{
var transactionHistoryList = new List<TransactionsHistory>();
transactionHistoryList.Add(new TransactionsHistory()
{
TransactionId = 50331,
Status = 2,
Comments = "Multiple entities",
});
context.TransactionsHistory.AddRange(transactionHistoryList );
var addedEntities = context.ChangeTracker.Entries()
.Where(x => x.State == EntityState.Added);
// checking the entity state of transactionHistoryList
if (context.Entry(transactionHistoryList).State == EntityState.Added) // exception
{
context.SaveChanges();
}
}
型号:
public partial class TransactionsHistory
{
public long TransactionId { get; set; }
public short? Status { get; set; }
public string Comments {get ;set;}
}
谁能帮我解决这个问题?我错过了什么吗?
提前致谢
在这种情况下,您在 addedEntities
中获得了来自 ChangeTracker
的所有 added
个实体,并且不需要在 if (context.Entry(transactionHistoryList).State == EntityState.Added)
.
context.Entry
接受一个 entity
但你将 entities
的范围传递给 context.Entry
你必须改成这个
context.TransactionsHistory.AddRange(transactionHistoryList);
var addedEntities = context.ChangeTracker.Entries()
.Any(x => x.State == EntityState.Added);
if (addedEntities)
{
context.SaveChanges();
}
更新
如果你想检查所有添加的实体,你可以使用这个
foreach(var entity in ChangeTracker.Entries())
{
if(entity.State = EntityState.Added)
{
//DoSomething
}
}