EF4 Code First:如何在不添加导航属性的情况下添加关系

发布于 2024-10-20 18:38:29 字数 1723 浏览 3 评论 0原文

我应该如何使用 Code First 但不使用任何导航属性来定义关系?

之前,我通过在关系的两端使用导航属性来定义一对多和多对多。并在数据库中创建适当的关系。这是类的精简版本(为了简单起见,我已将多对多关系转换为一对多关系)。

public class User 
{
    public string UserId { get; set; }
    public string PasswordHash { get; set; }
    public bool IsDisabled { get; set; }
    public DateTime AccessExpiryDate { get; set; }
    public bool MustChangePassword { get; set; }

    public virtual Role Role { get; set; }
}

public class Role
{
    public int RoleId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public virtual ICollection<User> Users { get; set; }
    public virtual ICollection<Right> Rights { get; set; }
}

public class Right
{
    public Guid RightId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public virtual Role Role { get; set; }
}

但是,如果我删除导航属性,则不会创建任何关系。以下是课程的样子。

public class User 
{
    public string UserId { get; set; }
    public string PasswordHash { get; set; }
    public bool IsDisabled { get; set; }
    public DateTime AccessExpiryDate { get; set; }
    public bool MustChangePassword { get; set; }

    public int Role RoleId { get; set; }
}

public class Role
{
    public int RoleId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

public class Right
{
    public Guid RightId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public int RoleId { get; set; }
}

请注意,我有相关表的主键,而不是导航属性。一切都在桌子上创建 - 除了关系。那么我该如何去做呢?

顺便说一句,我在 dbcontext 的 OnModelCreating 方法中尝试了各种组合,但没有成功。非常感谢任何帮助!

谢谢, 梅尔

How should I define relationships using Code First but without using any navigation properties?

Previously I have defined One-Many and Many-Many by using navigation properties in both ends of the relationship. And the appropriate relationships are created in the database. here's a stripped down version of how the classes look like (I've converted the Many-Many relationships to one-many for simplicity).

public class User 
{
    public string UserId { get; set; }
    public string PasswordHash { get; set; }
    public bool IsDisabled { get; set; }
    public DateTime AccessExpiryDate { get; set; }
    public bool MustChangePassword { get; set; }

    public virtual Role Role { get; set; }
}

public class Role
{
    public int RoleId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public virtual ICollection<User> Users { get; set; }
    public virtual ICollection<Right> Rights { get; set; }
}

public class Right
{
    public Guid RightId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public virtual Role Role { get; set; }
}

However, if I remove the navigation properties, no relationships are being created. Here's how the classes would look like.

public class User 
{
    public string UserId { get; set; }
    public string PasswordHash { get; set; }
    public bool IsDisabled { get; set; }
    public DateTime AccessExpiryDate { get; set; }
    public bool MustChangePassword { get; set; }

    public int Role RoleId { get; set; }
}

public class Role
{
    public int RoleId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

public class Right
{
    public Guid RightId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public int RoleId { get; set; }
}

notice that instead of a navigation property, I have the primary key of the related table. Everything gets created on the table - except the relationship. So how do I get about doing this?

BTW, I've tried various combinations in the OnModelCreating method of the dbcontext but to no avail. Any help is much appreciated!

Thanks,
Mel

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

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

发布评论

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

评论(3

嘦怹 2024-10-27 18:38:30

我相信在使用代码优先时,您总是至少在一侧需要导航属性。然后你就可以映射它:

public class User  
{     
    public string UserId { get; set; }     
    public string PasswordHash { get; set; }     
    public bool IsDisabled { get; set; }     
    public DateTime AccessExpiryDate { get; set; }     
    public bool MustChangePassword { get; set; }      
    public int RoleId { get; set; }
    public Role Role { get; set; }
}  

public class Role 
{     
    public int RoleId { get; set; }     
    public string Name { get; set; }     
    public string Description { get; set; } 
}  

public class Right 
{     
    public Guid RightId { get; set; }     
    public string Name { get; set; }     
    public string Description { get; set; }      
    public int RoleId { get; set; }
    public Role Role { get; set; }
} 

public class TestContext : DbContext
{
    public TestContext() : base("Entities")
    {}

    protected override void  OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<User>()
            .HasRequired(r => r.Role)
            .WithMany()
            .HasForeignKey(r => r.RoleId);

        modelBuilder.Entity<Right>()
            .HasRequired(r => r.Role)
            .WithMany()
            .HasForeignKey(r => r.RoleId);
    }
}

I believe you always need navigation property on at least one side when using code-first. Then you will be able to map it:

public class User  
{     
    public string UserId { get; set; }     
    public string PasswordHash { get; set; }     
    public bool IsDisabled { get; set; }     
    public DateTime AccessExpiryDate { get; set; }     
    public bool MustChangePassword { get; set; }      
    public int RoleId { get; set; }
    public Role Role { get; set; }
}  

public class Role 
{     
    public int RoleId { get; set; }     
    public string Name { get; set; }     
    public string Description { get; set; } 
}  

public class Right 
{     
    public Guid RightId { get; set; }     
    public string Name { get; set; }     
    public string Description { get; set; }      
    public int RoleId { get; set; }
    public Role Role { get; set; }
} 

public class TestContext : DbContext
{
    public TestContext() : base("Entities")
    {}

    protected override void  OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<User>()
            .HasRequired(r => r.Role)
            .WithMany()
            .HasForeignKey(r => r.RoleId);

        modelBuilder.Entity<Right>()
            .HasRequired(r => r.Role)
            .WithMany()
            .HasForeignKey(r => r.RoleId);
    }
}
绝影如岚 2024-10-27 18:38:30

您可以使用 Fluent api 添加关系,但“配置”代码与实体代码分开,使其不易被发现。

You can use the fluent api to add the relationship, though the "configuration" code is separate from the entity code making it less discoverable.

帅的被狗咬 2024-10-27 18:38:30

在 EF 4.3 中,您可以添加添加关系的迁移。

In EF 4.3, you can add a Migration that adds the relationship.

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