6

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

darewreck
  • 2,576
  • 5
  • 42
  • 67

1 Answers1

6

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
DDan
  • 8,068
  • 5
  • 33
  • 52
  • do youz have any idea why they made this design choice? It makes no sense in my eyes, especially if you can circumvent this by just adding once extra layer of inheritance.. – Squirrel in training Nov 12 '21 at 11:08