The entity type not part of the model for the current context c# entity-framework


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**

    //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 DbSet1 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.InternalSet1.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:");

    Console.WriteLine("\nOriginal values:");

    Console.WriteLine("\nDatabase values:");

Could u please help me with that? Thanks

10/3/2013 5:49:09 PM

Accepted Answer

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


This would work and it would log the added entity.

10/3/2013 7:39:58 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow