c# - 使用 Entity Framework 将记录插入表中

标签 c# entity-framework reflection

我有一个 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/

相关文章:

asp.net-mvc - 使用 IRepository 进行 Entity Framework 测试 - 延迟加载问题

scala - Scala 2.10 中编译器生成的隐式值是什么?

c# - 查询数据表字段包含列表中任意项的行<string>

c# - 不可变对象(immutable对象)是好的做法吗?

c# - Uri.Parse 已弃用

sql-server - 如何显示具有图像路径的 SQL 图像表中的图像

entity-framework - 如何记录 SQL 查询

c# - 二进制搜索与 LINQ select 语句

Java 反射 API。如果内部类不是公共(public)的,则无法实例化内部类

java - 如何使用反射包从类路径创建对象