我有一个 DbContext
对象,它有一个名为“Employees”的表。
我有一个员工对象,我想将其插入到 Employees
表中。
我不能直接从上下文中使用 Employees
。我想使用表名访问:这可能吗?
MyContext _ctx;
Type employee = a.GetType("SC.Employee"));
object employee = Newtonsoft.Json.JsonConvert.DeserializeObject(emp, employee);
using (_ctx = new MyContext())
{
//I am trying to insert the employee object to my entity. But this doesn't work
//_customCtx["Employees"].add(employee);
}
最佳答案
如果我理解你的问题,你想先将 Json 反序列化为 Employee 对象,然后将其保存到你的上下文中?
我建议你这样做
Employee myNewEmployee = JsonConvert.DeserializeObject<Employee>(json_string);
using (var _ctx = new MyContext())
{
_ctx.Employee.Add(myNewEmployee);
}
关于c# - 使用 Entity Framework 将记录插入表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39147813/