0

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() :(

Nate
  • 30,286
  • 23
  • 113
  • 184
  • 1
    The solution at http://stackoverflow.com/questions/480872/entity-framework-setting-a-foreign-key-property/ has fixed my compiler errors, but I'm still working through some other stuff errors. I'll see what I find. – Nate Jun 24 '10 at 20:47
  • Agree with marc_s; EF4 is much more production-ready. Upgrade if at all possible. – Stephen Cleary Jun 24 '10 at 20:58
  • This will require .NET4 -- correct? – Nate Jun 24 '10 at 21:19
  • 1
    @marc_s, you can easily find/set the IDs through the `EntityKey` property, if you care to, in 3.5 SP1. – Craig Stuntz Jun 25 '10 at 13:36

2 Answers2

0

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();
Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
0

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

Community
  • 1
  • 1
Nate
  • 30,286
  • 23
  • 113
  • 184