2017-03-02 14 views
0

私はAsp.netコアでEFを使用しましたが、アップデートしようとするとエラーが発生しました。Asp.netコアEFアップデート

「System.InvalidOperationException」種類の例外は Microsoft.EntityFrameworkCore.dllで発生したが、ユーザーコードで

追加情報処理されなかった:エンティティタイプのインスタンスを「TodoItem」 をので、追跡することができません同じキーを持つこのタイプの別のインスタンスはすでに です。新しいエンティティを追加する場合、ほとんどのキータイプについて、キーが設定されていない場合(つまり、 のキープロパティにそのタイプのデフォルト値が割り当てられている場合)、一意の一時キー値が作成されます( )。 が新しいエンティティのキ​​ー値を明示的に設定している場合は、 が既存のエンティティまたは他の 新しいエンティティ用に生成された一時的な値と衝突しないようにしてください。既存のエンティティをアタッチする場合は、指定されたキー値を持つエンティティインスタンスが1つだけコンテキストにアタッチされていることを確認してください。ここで

私の更新コードです:

public class TodoRepository : ITodoRepository 
{ 
    private readonly TodoContext _context; 

    public TodoRepository(TodoContext context) 
    { 
     _context = context; 
     //initialize database 
     Add(new TodoItem { Name = "Item1" }); 
     //Add(new TodoItem { Name = "Item2" }); 
     //Add(new TodoItem { Name = "Item3" }); 
    } 

    public IEnumerable<TodoItem> GetAll() 
    { 
     return _context.TodoItems.AsNoTracking().ToList(); 
    } 

    public void Add(TodoItem item) 
    { 
     _context.TodoItems.Add(item); 
     _context.SaveChanges(); 
    } 

    public TodoItem Find(long key) 
    { 
     return _context.TodoItems.AsNoTracking().FirstOrDefault(t => t.Key == key); 
    } 

    public void Remove(long key) 
    { 
     var entity = _context.TodoItems.AsNoTracking().First(t => t.Key == key); 
     _context.TodoItems.Remove(entity); 
     _context.SaveChanges(); 
    } 

    public void Update(TodoItem item) 
    { 
     _context.TodoItems.Update(item); 
     _context.SaveChanges(); 
    } 
} 

あなたは見つけることができるように、私はすでにAsNoTrackingを試してみましたが、私もStartup.csでそれを試してみました。

public void ConfigureServices(IServiceCollection services) 
{ 
    //inject repository into DI container, use database in memory 
    services.AddDbContext<TodoContext>(options => options.UseInMemoryDatabase().UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)); 

    //inject repository into DI container, and use sql databse 
    //services.AddDbContext<TodoContext>(options=>options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"])); 


    //The first generic type represents the type (typically an interface) that will be requested from the container. 
    //The second generic type represents the concrete type that will be instantiated by the container and used to fulfill such requests. 
    services.AddSingleton<ITodoRepository, TodoRepository>(); 

    //add mvc service to container, this is conventional routing 
    //This also applys to web api which is Attribute Routing 
    services.AddMvc(); 
} 

助けてください。

+0

私のコントローラにTodoContext todoContextを挿入し、 '_todoContext.TodoItems.Update(todoItem);を使用すると、 _todoContext.SaveChanges(); '、それは動作します。なぜ私のTodoRepositoryでうまくいかなかったのかわかりません。 –

+0

DBエントリの状態を変更しようとしましたか? 44行https://github.com/hherzl/Northwind/blob/master/SourceCode/Northwind.Core/DataLayer/Repository.cs –

+0

あなたはメモリ内で作業していますか? Todoitem.Keyの値はどのように生成されますか? InMemoryはシーケンスサポートを持っていないので、どこにも割り当てていなければ、すべてのTodoItemはKey == 0です。 – Dmitry

答えて