一尘不染

EF4代码优先:如何在不添加导航属性的情况下添加关系

c#

我应该如何使用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方法中的各种组合,但无济于事。任何帮助深表感谢!

谢谢,梅尔


阅读 368

收藏
2020-05-19

共1个答案

一尘不染

我相信在使用代码优先时,您始终总是需要至少一侧的导航属性。然后您将能够映射它:

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);
    }
}
2020-05-19