2013-10-03 102 views
0

我正在嘗試記錄costumer詳細信息表中的更改。對於我使用EF4實體類型不是當前上下文的模型的一部分

using (realstateEntities context = new realstateEntities()) 
{ 
    //Here the model is built** 
    cadClientes cliente = new cadClientes(); 
    cliente.Nome = model.nome; 
    ... 
    cliente.observacao = model.observacao; 

    //Here I am adding the model and saving the changes** 
    context.cadClientes.Add(cliente); 
    context.SaveChanges();         

    //Now I am trying to log that operation** (Error is in following line) 
    paramsOriginais = LogsController.PrintProperties("cadClientes", context.Entry(context.cadClientes).GetDatabaseValues()); 

} 

,我得到這個錯誤:

System.InvalidOperationException: The entity type DbSet 1 is not part of the model for the current context.\r\n at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)\r\n at System.Data.Entity.Internal.Linq.InternalSet 1.Initialize()\r\n at System.Data.Entity.DbContext.Entry[TEntity](TEntity entity)\r\n at realstate.Controllers.CadastrosController.clienteAdd(ClientesModel model, FormCollection form) in c:\Users\guilherme\Documents\Visual Studio 2012\Projects\realstate\realstate\Controllers\CadastrosController.cs:line 303"

我寫了一個基於從微軟的網站這個例子中我的代碼:

using (var context = new UnicornsContext()) 
{ 
    var unicorn = context.Unicorns.Find(1); 

    // Make a modification to Name in the tracked entity 
    unicorn.Name = "Franky"; 

    // Make a modification to Name in the database 
    context.Database.SqlCommand("update Unicorns set Name = 'Squeaky' where Id = 1"); 

    // Print out current, original, and database values 
    Console.WriteLine("Current values:"); 
    PrintValues(context.Entry(unicorn).CurrentValues); 

    Console.WriteLine("\nOriginal values:"); 
    PrintValues(context.Entry(unicorn).OriginalValues); 

    Console.WriteLine("\nDatabase values:"); 
    PrintValues(context.Entry(unicorn).GetDatabaseValues()); 
} 

可以ü請幫助我呢?謝謝

回答

4

它是由context.Entry(context.cadClientes)造成的。

cadClientescadCliente實體的DbSet。實體 - cadCliente - 是模型的一部分,DbSet不是。我認爲你想要做的是

context.Entry(cliente) 

這將工作,它會記錄添加的實體。

相關問題