Question

So, In my old Linq-To-SQL I would write code like this

var tableItem = new Table
{
    Prop1 = var1
    Prop2 = var2,
    Prop3 = var3,
    ParentTableID = parentRecordID
};

db.Table.InsertOnSubmit(tableItem);
db.SubmitChanges();

After converting my working code from Linq-To-SQL to Entity Framework (v3.5) the ParentTableID property is no longer there and I'm at a loss as to how I can create this related child record.

I'm not sure what I should change for my entity framework version, besides the obvious call to SaveChanges(); instead of SubmitChanges() :(

Was it helpful?

Solution 2

I ended up using the solution here: Entity Framework: Setting a Foreign Key Property.

OTHER TIPS

For .NET 4, you can use FK associations.

For .NET 3.5 SP1 (I'm guessing at your property names; fix it if I guess wrong):

var tableItem = new Table
{
    Prop1 = var1
    Prop2 = var2,
    Prop3 = var3,
    ParentTable = db.Table.Where(t => Id == parentRecordID).First();
};

db.Table.AddObject(tableItem);
db.SaveChanges();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top