开发者

Switching from Linq-To-SQL to Entity Framework for WCF Data Services Issue with FK "Properties"

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 ca开发者_如何学Cn 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() :(


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜