实体框架 4.1 中具有不同名称的唯一列和导航属性的流畅映射/数据注释?

发布于 2024-11-04 23:09:51 字数 507 浏览 5 评论 0原文

首先,有没有办法通过使用数据注释或流畅的 API 来告诉 EF 4.1 列必须是唯一的?

其次,关于导航属性,我有两个类 Document 和 User。在文档类中,我有一个 OwnerId (UserId) 属性和一个 Owner (User) 属性。如何告诉 EF 4.1 OwnerId 实际上是 UserId 并且 Owner 是返回到 User 的导航属性?

文档类:

public abstract class Document: BaseEntity
    {
        public bool IsActive { get; set; }
        public string Description { get; set; }
        //The UserId
        public Guid OwnerId { get; set; }
        //The User
        public User Owner { get; set; }

    }

First, is there a way to tell EF 4.1 that a column needs to be unique by using either data annotations or the fluent API?

Second, regarding navigation properties, I have have 2 classes Document and User. In the document class I have a property for the OwnerId (UserId) and a property for Owner (User). How do I tell EF 4.1 that the OwnerId is really UserId and Owner is a navigation property back to User?

The Document Class:

public abstract class Document: BaseEntity
    {
        public bool IsActive { get; set; }
        public string Description { get; set; }
        //The UserId
        public Guid OwnerId { get; set; }
        //The User
        public User Owner { get; set; }

    }

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

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

发布评论

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

评论(1

情徒 2024-11-11 23:09:54

实体框架根本不支持唯一键,因此第一个问题的答案是否定的。

OwnerId 应自动识别为 Owner 的外键,除非您映射到外键名称不同的现有数据库。在这种情况下,您可以使用例如:

public abstract class Document: BaseEntity
{
    public bool IsActive { get; set; }
    public string Description { get; set; }
    [Column("UserId"), ForeignKey("Owner")]
    public Guid OwnerId { get; set; }
    public User Owner { get; set; }
}

可能不需要外键数据注释,但您可以使用它来显式地将 FK 属性与导航属性配对。

在流畅的映射中,您可以使用:

modelBuilder.Entity<Document>()
            .Property(d => d.OwnerId)
            .HasColumnName("UserId");
modelBuilder.Entity<Document>()
            .HasRequired(d => d.Owner)
            .WithMany(...)
            .HasForeignKey(d => d.OwnerId);

Entity framework doesn't support unique keys at all so the answer to your first question is no.

OwnerId should be recognized as foreign key for Owner automatically unless you map to existing database where the foreign key is named differently. In such case you can use for example:

public abstract class Document: BaseEntity
{
    public bool IsActive { get; set; }
    public string Description { get; set; }
    [Column("UserId"), ForeignKey("Owner")]
    public Guid OwnerId { get; set; }
    public User Owner { get; set; }
}

Foreign key data annotation is probably not needed but you can use it to explicitly pair your FK property with navigation property.

In fluent mapping you can use:

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