1

我正在尝试记录客户详细信息表中的更改。为此,我正在使用 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:实体类型 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.InternalSet1.Initialize()\r\n 在 System.Data.Entity.DbContext.Entry[TEntity](TEntity entity)\r\n 在 realstate.Controllers.CadastrosController.clienteAdd(ClientesModel model, FormCollection 表单)在 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

1 回答 1

6

它是由context.Entry(context.cadClientes).

cadClientes是一个DbSet实体cadCliente。实体 - cadCliente- 是模型的一部分,DbSet不是。我想你打算做的是

context.Entry(cliente)

这会起作用,它会记录添加的实体。

于 2013-10-03T19:39:58.947 回答