kirk
kirk

Reputation: 1007

Choose table to insert into using Linq

I just want to ask if this is possible in Linq

I have 3 identical tables:

TableA           TableB          TableC
Name             Name            Name
Age              Age             Age

before inserting the values, i need to choose what table to insert into. The condition should be like this:

var TableABC = new Table;
if(str == 'a')
{
   TableABC = TableA;
}
else if (str == 'b')
{
   TableABC = TableB;
}
else
{
   TableABC = TableC;
}

var Save = new TableABC 
{
    Name = 'John Smith',
    Age = 30
}

Context.Table.AddObject(TableABC);
Context.SaveChanges();

Upvotes: 1

Views: 97

Answers (2)

kirk
kirk

Reputation: 1007

I found a solution.. Instead of using var i used dynamic..

var TableABC = new Table;

to

dynamic TableABC;

Upvotes: 1

idipous
idipous

Reputation: 2910

It would more helpful to create entity objects and use the objects like below

switch(str)
  case 'a':
    context.EntityA.Add(obj);
    context.SaveChanges();
   break;
 etc...

Now about how to create entity objects (which is very easy as they are plain classes) and use EF and create context you can ready this tutorial:

http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

Upvotes: 0

Related Questions