views:

264

answers:

1

I have a table (TestTable) for example:

ID
Name
Parentid (FK)

and I would like to insert a new record like ID(1) Name(Test) ParentID(5) FK. How can insert a new record into TestTable with linq to entity?

var testTable = new TestTable();
testTable.ID = 1;
testTable.Name = "TestName";
testTable ...

thank you for the working example.

A: 

Try this:

using (var context = new MyEntities())//MyEntities name may differ
{
    var testTable = new TestTable();
    testTable.ID = 1;
    testTable.Name = "TestName";
    testTable.ParentTableReference.EntityKey = EntityKey("MyEntities.ParentTableSet", entityKeyValues);//Entity set name for parent table may differ

    context.AddObject("TestTableSet",testTable);//Entity set name for TestTable may differ
    context.SaveChanges();
}
LukLed