简体   繁体   English

实体框架 6 - 使用具有三个表的类(数据库优先)

[英]Entity Framework 6 - using a class with three tables (Db first)

I'm working with Entity Framework 6.我正在使用实体框架 6。

I have three tables in my database:我的数据库中有三个表:

  • Payments付款
  • Payments_temp Payments_temp
  • Payments_error Payments_error

Payments has these columns: Payments有这些列:

  • Id ID
  • Amount数量
  • Json杰森

The other two tables have the same columns, but with a column called Result.其他两个表具有相同的列,但有一个名为 Result 的列。

Can I create a class Payment with all columns and then mapping in the database just the properties I need from a case?我可以创建一个包含所有列的Payment类,然后在数据库中仅映射我需要的案例属性吗?

Example:例子:

public class Payment
{
    public string id {get;set;}
    public decimal amount {get;set;}
    public string json {get;set;}
    public string result {get;set;}
}

And in DbContext :DbContext

Using(var context = new dbcontext())
{
     paym = new Payment();
     context.Payments.add(paym);

     paym.result = "OK";
     context.Payments_temp.add(paym);
     context.Payments_error.add(paym);
}

If someone knows how do that, I'd really appreciate it如果有人知道怎么做,我真的很感激

Solved: Convert the clases with json serialize.解决:用json序列化转换类。

In DBContext在数据库上下文中

  {
     paym = new Payments();
     context.Payments.add(paym);
     paym.result = "OK";
     var serializedPayments = JsonConvert.SerializeObject(paym);
     context.Payments_temp
             .add(JsonConvert.DeserializeObject<Payments_temp>(paym));
     context.Payments_error
             .add(JsonConvert.DeserializeObject<Payments_error>(paym));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 首先是数据库表子集的实体框架代码 - Entity framework code first for the subset of DB tables Entity Framework Core DB首先将列或表映射到具有不同名称的属性或类 - Entity Framework Core DB first Mapping columns or tables into properties or class with different names 如何使用Entity Framework Code First CTP5按年份查询相同的数据库表? - How do I query identical DB tables by year using Entity Framework Code First CTP5? 是否可以使用实体框架数据库优先方法从数据库表创建的模型之外创建视图模型? - Is it possible to create view models by out of models created from database tables using entity framework DB first approach? 使用实体框架加载三个表包括和延迟加载 - Loading three tables using Entity Framework include and lazy loading 实体框架加入三个表并使用组Concat - Entity Framework Joining Three Tables and Using Group Concat 在数据库优先方法中为 Entity Framework 5 中的所有实体创建一个 Base Class - Creating a Base Class for all entities in Entity Framework 5 in DB first approach 实体框架Db First和Enum - Entity Framework Db First and Enum 子对象未保存到数据库中,先使用实体​​框架代码 - Child object not saving into the DB, using Entity Framework Code First 三张表与实体框架之间的关系 - Relationships between three tables with Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM