针对 WCF 数据服务从 Linq-To-SQL 切换到实体框架 FK“属性”问题

发布于 2024-09-07 07:17:45 字数 419 浏览 1 评论 0原文

因此,在我的旧 Linq-To-SQL 中,我会编写这样的代码

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

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

将我的工作代码从 Linq-To-SQL 转换为实体框架 (v3.5) 后,ParentTableID 属性不再存在,并且我不知道如何创建这个相关的子记录。

除了明显调用 SaveChanges(); 而不是 SubmitChanges() :(

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

探春 2024-09-14 07:17:45

对于 .NET 4,您可以使用 FK 关联。

对于.NET 3.5 SP1(我猜测你的属性名称;如果我猜错了,请修复它):

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

db.Table.AddObject(tableItem);
db.SaveChanges();

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 2024-09-14 07:17:45

我最终在这里使用了解决方案: 实体框架:设置外部关键属性

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文