【问题标题】:How to fix this error: 'The instance of entity type cannot be tracked...'如何修复此错误:“无法跟踪实体类型的实例...”
【发布时间】:2019-12-10 06:28:28
【问题描述】:

这里是完整的错误:

System.InvalidOperationException:“无法跟踪实体类型‘Book’的实例,因为已经在跟踪另一个具有键值‘{BookId: 382234c3-721d-4f34-80e5-57657bbbbb32}’的实例。附加现有实体时,确保只附加一个具有给定键值的实体实例。”

当我尝试从业务逻辑层中的服务向我的数据库添加新实体时,它会抛出。我使用了 EF Core,制作了存储库和工作单元,以便在我的数据访问层中与 DB 进行交互。

这是一些引发错误的代码,它在存储库中:

public void Create(T entity)
{
    this.LibraryContext.Set<T>().Add(entity);
}

反过来,它是从这里调用的:

public bool TakeBookById(Guid bookId, Guid studentId, DateTime issueDate) // student takes the book from library
        {
            var book = mapper.Map<BookDto>(unit.Books.FindByCondition(x => x.BookId == bookId).FirstOrDefault()); // take that book for later using 

            if (book != null && book.IsAvailable == true) // if this book is available
            {
                var student = mapper.Map <StudentDto>(unit.Students.FindByCondition(x => x.StudentId == studentId).FirstOrDefault()); // look for that student
                if (student != null && student.LibraryCard.Fields.Where(x => x.Book.IsAvailable == false).Count() <= 10)
                { // if student exists and his/her limit of non-returned books is not exceeded
                    var libraryCard = mapper.Map<LibraryCardDto>(student.LibraryCard); // take his/her library card
                    unit.LibraryCardFields.Create( // create library card's field with current data
                        mapper.Map<LibraryCardField> ( 
                            new LibraryCardFieldDto()
                            {
                                Id = Guid.NewGuid(), // generate new id
                                Book = book, // student got this book
                                LibraryCard = libraryCard, // field is in this library card
                                IssueDate = issueDate, // note current date
                                ReturnDate = null // student still didn't return it
                            } )
                    ); 

                    unit.Save(); // save changes
                    return true;
                }
                else { return false; }    
            }
            else { return false; }    
        }

这是我调用服务方法的代码,为了清楚起见,尽可能硬编码:

StudentService sts = new StudentService("Server=.\\SQLEXPRESS;Database=StudentsLibrary;Trusted_Connection=True;");

sts.TakeBookById(Guid.Parse("382234C3-721D-4F34-80E5-57657BBBBB32"), Guid.Parse("382C74C3-721D-4F34-80E5-57657B6CBC27"), DateTime.Now);

我还应用了我从存储库中使用的方法:

public IEnumerable<T> FindAll()
{
     return this.LibraryContext.Set<T>();
}
public IEnumerable<T> FindByCondition(Expression<Func<T, bool>> expression)
{
     return this.LibraryContext.Set<T>().Where(expression);
}

我没有在存储库中使用 AsNoTracking(),因为我希望延迟加载工作(我不想为每个模型编写所有 Inlude&ThenInclude 内容)。

我想,如果专门跟踪这本书,那么我可以让上下文停止跟踪它。我写了这样的东西:

public void Reset()
{
     var entries = libraryContext.ChangeTracker.Entries().ToArray();
     foreach (var entry in entries)
     {
         entry.State = EntityState.Detached;
     }
}

并尝试了这个:

public void Reset()
{
     libraryContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
}

你知道吗?仍然存在该错误。我不知道这可能是因为映射器?

更新:这是因为映射器。但是我该如何解决这种情况呢?如果在 Create 方法中创建我的 DAL 模型的对象,它会起作用。我无法创建我的 BLL 模型的对象然后对其进行映射。 EF 受不了这些东西。

请帮帮我。我是 EF 新手。

【问题讨论】:

  • 告诉我你在哪里配置DTOEntity

标签: c# entity-framework asp.net-core .net-core entity-framework-core


【解决方案1】:

放下自动映射器。只需几秒钟即可输入您自己的扩展方法,并且您可以轻松完全控制它。

您的主要问题是您创建了一个 DTO 然后将其转换为一个实体并且您没有正确配置它。完全跳过该部分并创建实体。像这样使用DTO 是没有意义的。查看下面的代码。

 unit.LibraryCardFields.Create( // create library card's field with current data
                        mapper.Map<LibraryCardField> ( 
                            new LibraryCardFieldDto()
                            {
                                Id = Guid.NewGuid(), // generate new id
                                Book = book, // student got this book
                                LibraryCard = libraryCard, // field is in this library card
                                IssueDate = issueDate, // note current date
                                ReturnDate = null // student still didn't return it
                            } )
                    ); 

更改此项以创建您自己的扩展方法并自行转换。

以这个为例。

假设您传入了一个 DTO 而不是创建它。

var LibraryCardField = LibraryCardFeildDto.ToEntity();//ToEntity is your extension method
 unit.LibraryCardFields.Create(LibraryCardField);

为扩展方法创建一个新类。

public static class ExampleMapperClass //or w/e you want to call it{

  public static LibaryCardField ToEntity(this LibraryCardFieldDto lbf){
      return new LibraryCardField{
        Id = lbf.Id,
        Book = lbf.book.ToEntity(),//create another extension method for this one
        LibraryCard  = LibraryCardDto.ToEntity(),//same here
        IssueDate = lbf.issueDate,
        ReturnDate = lbf.returnDate
      };
  }  
}

【讨论】:

    猜你喜欢
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    相关资源
    最近更新 更多