实体框架:设置外键属性

发布于 07-12 08:29 字数 455 浏览 12 评论 0原文

我们有一个大致如下所示的表:

CREATE TABLE Lockers 
{
  UserID int NOT NULL PRIMARY KEY (foreign key),
  LockerStyleID int (foreign key),
  NameplateID int (foreign key)
}

所有键都与其他表相关,但由于应用程序的分发方式,我们更容易将 ID 作为参数传递。 所以我们想要这样做:

Locker l = new Locker { 
  UserID = userID, 
  LockerStyleID = lockerStyleID, 
  NameplateID = nameplateID 
};
entities.AddLocker(l);

我们可以在 LINQ-to-SQL 中完成,但不能在 EF 中完成?

We have a table that looks roughly like this:

CREATE TABLE Lockers 
{
  UserID int NOT NULL PRIMARY KEY (foreign key),
  LockerStyleID int (foreign key),
  NameplateID int (foreign key)
}

All of the keys relate to other tables, but because of the way the application is distributed, it's easier for us to pass along IDs as parameters. So we'd like to do this:

Locker l = new Locker { 
  UserID = userID, 
  LockerStyleID = lockerStyleID, 
  NameplateID = nameplateID 
};
entities.AddLocker(l);

We could do it in LINQ-to-SQL, but not EF?

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

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

发布评论

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

评论(6

无所的.畏惧2024-07-19 08:29:20

这个缺失的功能似乎让很多人烦恼。

  • 好消息:MS 将通过 .NET 4.0 解决该问题。
  • 坏消息:目前,或者如果您停留在 3.5 上,您必须做一些工作,但这是可能的。

你必须这样做:

Locker locker = new Locker();
locker.UserReference.EntityKey = new System.Data.EntityKey("entities.User", "ID", userID);
locker.LockerStyleReference.EntityKey = new EntityKey("entities.LockerStyle", "ID", lockerStyleID);
locker.NameplateReference.EntityKey = new EntityKey("entities.Nameplate", "ID", nameplateID);
entities.AddLocker(locker);
entities.SaveChanges();

This missing feature seems to annoy a lot of people.

  • Good news: MS will address the issue with .NET 4.0.
  • Bad news: for now, or if you're stuck on 3.5 you have to do a little bit of work, but it IS possible.

You have to do it like this:

Locker locker = new Locker();
locker.UserReference.EntityKey = new System.Data.EntityKey("entities.User", "ID", userID);
locker.LockerStyleReference.EntityKey = new EntityKey("entities.LockerStyle", "ID", lockerStyleID);
locker.NameplateReference.EntityKey = new EntityKey("entities.Nameplate", "ID", nameplateID);
entities.AddLocker(locker);
entities.SaveChanges();
战皆罪2024-07-19 08:29:20

为了使事情变得简单,我自己在部分类中添加了外键属性:

public int UserID
{
   get
   {
      if (this.User != null)
         return this.User.UserID;
   }
   set 
   {
      this.UserReference.EntityKey = new System.Data.EntityKey("entities.User", "ID", value);
   }
}

What I've been doing to make things easy is adding the foreign key property myself in the partial class:

public int UserID
{
   get
   {
      if (this.User != null)
         return this.User.UserID;
   }
   set 
   {
      this.UserReference.EntityKey = new System.Data.EntityKey("entities.User", "ID", value);
   }
}
初见你2024-07-19 08:29:20

您可以创建一个扩展方法来根据这些 ID 构造实体。

You could make an extension method that constructs the entity based on these ID's.

忆梦2024-07-19 08:29:20

使用 EntityKey 可以解决您的问题;)

alk。

Using an EntityKey solves your problem ;)

alk.

梦情居士2024-07-19 08:29:20

如果您不介意“污染”数据库模式,另一种方法是添加计算列,例如,如果您有外键字段 FK_Customer,您可以定义一个新的计算列 FK_Customer_Compulated,其中包含表达式 FK_Customer。 当您生成\更新 edmx 模型时,该字段将显示为常规字段,然后您可以从实体对象中引用该字段。

或者等待 EF4 :)

another method if you don't mind 'polluting' you db schema is to add a computed column, e.g. if you had a foreign key field FK_Customer you could define a new computed column FK_Customer_Computed which has the expression FK_Customer. When you generate\update your edmx model the field will appear like a regular field that you can then reference from you entity object.

Or wait for EF4 :)

娇纵2024-07-19 08:29:20

继 Dylan 的回答之后,Alex James 专门写了一篇博客,详细解释了这个问题以及如何进行部分类 + 属性解决方案。

伪造外键 - EF3.5

Following on from Dylan's answer, Alex James has written a blog on precisely this, explaining the problem in full and how to go about the partial class + property solution.

Faking Foreign Keys - EF3.5

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