实体框架4.1外键问题
我正在 EF 4.1 Code First 中艰难地进行电子商务数据库设计。
我遇到了一种情况,我认为我是合理的,但不确定。
考虑:
class Download
{
int ID
string Mime
string Filename
string Extension
}
class DownloadBinary
{
int ID
int DownloadID
Download Download
byte[] Binary
}
class DownloadURL
{
int ID
int DownloadID
Download Download
string URL
}
现在我已将用于下载的外键放在其他两个类中,因为我希望基本上删除空值。此外,这还有一个副产品,即允许每个 Download 类有多个 DownloadBinary 和 DownloadURL 类,这似乎没问题。但从 EF 的角度来看,这似乎是错误的,因为 Download 类没有封装 DownloadBinary 和 DownloadURL 类。
我知道我可以搜索 DbContext 并退休 DownloadBinary 和 DownloadURL 类以获取给定的下载类 ID,这样我就可以正常获取数据。
如果我在 Download 类中保存 DownloadBinary ID 和 DownloadURL ID,我可能会受到 null 的影响,这就是我这样做的目的。
我真的希望从 Download 到 DownloadBinary 强制执行一对零或一对一的关系,或者我的数据输入表单中的 DownloadURL 类..
我在这里没有看到太大的问题,但是更有经验的人认为如何?
I'm trudging ahead with an ecommerce database desgin in EF 4.1 Code First.
I've come to a situation where I think I'm justified, but not sure..
Consider:
class Download
{
int ID
string Mime
string Filename
string Extension
}
class DownloadBinary
{
int ID
int DownloadID
Download Download
byte[] Binary
}
class DownloadURL
{
int ID
int DownloadID
Download Download
string URL
}
Now I've placed the Foreign Keys for Download in the other two classes as I wish to remove nulls basically. Also this has the byproduct of allowing multiple DownloadBinary and DownloadURL classes per Download class, which seems ok. But this seems the wrong way round from an EF point of view, as the Download class does not encapsulate the DownloadBinary and DownloadURL classes.
I know I can search the DbContext and retireve DownloadBinary and DownloadURL classes for a given Download class ID, so i can get data out ok.
If I held DownloadBinary ID and DownloadURL ID in the Download class, I could be subject to nulls, which is my point for doing it this way..
I'm really expecting to enforce a one to zero or one relationship from Download to DownloadBinary or DownloadURL classes in my data input forms..
I don't see much of a problem here, but what do more experienced people think??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从
DownloadBinary
和DownloadUrl
中删除DownloadID
并将这些类的ID
映射为ID< 的外键
下载
的/code>。 EF 仅支持主键上的一对一关系,因为它不支持唯一键。DownloadBinary
和DownloadUrl
都不能使用数据库中自动生成的 Id,因为它们的 ID 是由Download
定义的:modelBuilder.Entity()
.Property(db => db.ID)
.HasDatabaseGenerateOption(DatabaseGenerateOption.None);
Remove
DownloadID
from bothDownloadBinary
andDownloadUrl
and mapID
of these classes as foreign key toID
ofDownload
. EF supports one-to-one relation only over primary keys because it doesn't support unique keys.Both
DownloadBinary
andDownloadUrl
must not use autogenerated Id in database because their ID is defined byDownload
:modelBuilder.Entity()
.Property(db => db.ID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);