Reputation: 7493
I have over 10 SQL Tables with the same column structure but different table names. I want to be able to use LINQ to work with the data in the tables. Is it possible to create a class that models the table structure like I normally do but just not define the table name? Is there a way to use LINQ with tables that have the same structure but different names?
Upvotes: 4
Views: 1044
Reputation: 100328
With EF Code First you can map the entity to different tables dynamically:
public class OrderContext : DbContext
{
private readonly string _tableName;
public OrderContext(string tableName)
: base("name=OrderContext")
{
_tableName = tableName;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>().ToTable(_tableName);
base.OnModelCreating(modelBuilder);
}
public ICollection<Order> Orders { get; set; }
}
Usage:
var orders = new OrderContext("Orders").Orders;
var legacyOrders = new OrderContext("LegacyOrders").Orders;
Upvotes: 2