实体框架4.1外键问题

发布于 2024-12-06 12:15:49 字数 869 浏览 6 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(1

蒲公英的约定 2024-12-13 12:15:49

DownloadBinaryDownloadUrl 中删除 DownloadID 并将这些类的 ID 映射为 ID< 的外键下载的/code>。 EF 仅支持主键上的一对一关系,因为它不支持唯一键。

modelBuilder.Entity<Download>()
            .HasOptional(d => d.DownloadBinary)
            .WithRequired(db => db.Download);


modelBuilder.Entity<Download>()
            .HasOptional(d => d.DownloadUrl)
            .WithRequired(du => du.Download);

DownloadBinaryDownloadUrl 都不能使用数据库中自动生成的 Id,因为它们的 ID 是由 Download 定义的:
modelBuilder.Entity()
.Property(db => db.ID)
.HasDatabaseGenerateOption(DatabaseGenerateOption.None);

modelBuilder.Entity<DownoladUrl>()
            .Property(du => du.ID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);   

Remove DownloadID from both DownloadBinary and DownloadUrl and map ID of these classes as foreign key to ID of Download. EF supports one-to-one relation only over primary keys because it doesn't support unique keys.

modelBuilder.Entity<Download>()
            .HasOptional(d => d.DownloadBinary)
            .WithRequired(db => db.Download);


modelBuilder.Entity<Download>()
            .HasOptional(d => d.DownloadUrl)
            .WithRequired(du => du.Download);

Both DownloadBinary and DownloadUrl must not use autogenerated Id in database because their ID is defined by Download:
modelBuilder.Entity()
.Property(db => db.ID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

modelBuilder.Entity<DownoladUrl>()
            .Property(du => du.ID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文