实体框架6来自同一个实体对象创建两个表 [英] Entity Framework 6 Creating Two table from the same entity object

查看:238
本文介绍了实体框架6来自同一个实体对象创建两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能从一个定义的实体对象类创建两个表实例。



例如:

 公共类EntityA()
{
公共字符串名称{;设置;}
公共字符串值{搞定;设置;}
}

公共类MyDbConext:的DbContext
{
公共DbSet< EntityA>实例1 {搞定;组; }
公共DbSet< EntityA> INSTANCE2 {搞定;组; }
}



我想要做的就是创建一个实体的两个实例与不同的表名。这是可能的代码第一个实体框架?

$:我觉得这是似乎有些单调乏味有只创建一个扩展实体EntityA只创建同一个实体的另一个实例另一类



所需的输出b
$ b


  • 从EntityA类

  • 从EntityA级Instance2_Table创作
  • $ bInstance1_Table创作$ b


在DBConext的代码将抛出异常。



任何意见赞赏,
谢谢,
D


解决方案

您不能有多个 DbSet 指向同一个类中有一个的DbContext 。你的选择是:




  • 创建具有相同属性的新类

  • 继承

  • 使用不同的的DbContext 取值


I was wondering if it's possible to create two table instance from one defined entity object class.

Example:

public class EntityA()
{
    public String name {get; set;}
    public String value {get; set;}
}

public class MyDbConext : DbContext
{
    public DbSet<EntityA> instance1{ get; set; }
    public DbSet<EntityA> instance2{ get; set; }
}

What i'm trying to do is create two instances of Entity A with different table names. Is that possible with code first entity framework? I feel like it's seems tedious to have to just create another class that extends entity EntityA to just create another instance of the same entity.

Desired Output:

  • Creation of "Instance1_Table" from EntityA class
  • Creation of "Instance2_Table" from EntityA class

The code in the DBConext will throw an exception.

Any advice appreciated, Thanks, D

解决方案

You cannot have multiple DbSet pointing to the same class in one DbContext. Your options are:

  • Creating new class with same properties
  • Inheritance
  • Using different DBContexts

这篇关于实体框架6来自同一个实体对象创建两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆