I am trying to log changes in costumer details table. For that I am using 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());
}
and I get this error:
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"
I wrote my code based in this example from microsoft website:
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());
}
Could u please help me with that? Thanks
It is caused by context.Entry(context.cadClientes)
.
cadClientes
is a DbSet
of cadCliente
entities. The entity - cadCliente
- is part of the model, the DbSet
isn't. I think what you intended to do is
context.Entry(cliente)
This would work and it would log the added entity.