在实体框架中添加复杂对象

我正在设置.NET Core API。我的应用程序分为3层:

  • 数据访问
  • 业务逻辑/服务
  • API

我在使用API​​方法添加相关对象时遇到麻烦。假设我有以下课程。

public class Part
{
    [Key]public int Id {get; set;}
    public string Name {get; set;}
    public string ICollection<PartAttribute> PartAttributes{get; set;}
}

public class PartAttribute
{
    [Key]public int Id {get; set;}
    public string Name {get; set;}
    public string Value {get; set;}
}

以下与数据库交互的方法-上下文是EF DbContext:

public virtual void Add(T entity)
{
    Context.Set<T>()
        .Add(entity);
}

如果我通过API发送以下JSON(假设PartAttributes中的以下记录已存在),则无法添加已经具有PartAttributes的Part

{
        "Name":"Example1","PartAttributes":[
        {
            "attributeId":1,"name":"Color","value":"black"
        }]
}

我遇到以下异常:“当IDENTITY_INSERT设置为OFF时,无法在表'PartAttributes'中为标识列插入显式值。” -这使我得出结论,EF无法识别现有记录,而是尝试将其插入为新记录。由于SQL Server本身中的标识插入设置,导致失败。

我想要实现的是将识别现有对象,并且EF不会尝试将现有属性作为新记录插入数据库中。实现该行为的最佳实践是什么?

jackybye 回答:在实体框架中添加复杂对象

您需要使用主键(这里看起来是attributeId)并使用dbContext加载记录。加载后,更新值并在上下文中调用SaveChanges()SaveChangesAsync()

您甚至可以使用一种可以插入或更新的方法。

例如(SUDO CODE!):

public void InsertOrUpdate(PartAttribute model)
{
  var existingRecord = dbContext.Set<PartAttribute>().Find(model.attributeId);
  if(existingRecord == null)
  { // Insert (Should not include value for attributeId)
     dbContext.Add(model);
  }
  else
  { // Update
    existingRecord = model;
    dbContext.SaveChanges();
  }
}

如果您仍有问题,请告诉我

本文链接:https://www.f2er.com/3128543.html

大家都在问