C# 实体框架:设置外键属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/480872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Entity Framework: Setting a Foreign Key Property
提问by Rob
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:
所有键都与其他表相关,但由于应用程序的分布方式,我们更容易将 ID 作为参数传递。所以我们想这样做:
Locker l = new Locker {
UserID = userID,
LockerStyleID = lockerStyleID,
NameplateID = nameplateID
};
entities.AddLocker(l);
We could do it in LINQ-to-SQL, but not EF?
我们可以在 LINQ-to-SQL 中做到这一点,但不能在 EF 中做到?
采纳答案by Pieter Breed
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.
- 好消息:微软将在 .NET 4.0 中解决这个问题。
- 坏消息:现在,或者如果你被困在 3.5 上,你必须做一些工作,但这是可能的。
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();
回答by driAn
You could make an extension method that constructs the entity based on these ID's.
您可以创建一个基于这些 ID 构造实体的扩展方法。
回答by driAn
Using an EntityKey solves your problem ;)
使用 EntityKey 可以解决您的问题;)
alk.
吧。
回答by Dylan Vester
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);
}
}
回答by Jason Roberts
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.
如果您不介意“污染”您的数据库架构,另一种方法是添加一个计算列,例如,如果您有一个外键字段 FK_Customer,您可以定义一个新的计算列 FK_Customer_Computed,它具有表达式 FK_Customer。当您生成\更新您的 edmx 模型时,该字段将显示为常规字段,然后您可以从实体对象中引用该字段。
Or wait for EF4 :)
或等待 EF4 :)
回答by ChirpingPanda
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.
继 Dylan 的回答之后,Alex James 就这一点写了一篇博客,完整解释了这个问题以及如何进行部分类 + 属性解决方案。