【问题标题】:EF4 Code First: how to add a relationship without adding a navigation propertyEF4 Code First:如何在不添加导航属性的情况下添加关系
【发布时间】:2011-07-10 04:49:17
【问题描述】:

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

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

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 方法中尝试了各种组合,但无济于事。非常感谢任何帮助!

谢谢, 梅尔

【问题讨论】:

    标签: c# entity-framework entity-framework-4 code-first ef-code-first


    【解决方案1】:

    您可以使用fluent api 添加关系,但“配置”代码与实体代码是分开的,因此不太容易被发现。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 这个答案将受益于如何使用该功能的示例。
      【解决方案3】:

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

      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);
          }
      }
      

      【讨论】:

      • 我也有同样的怀疑。谢谢你的例子。我曾尝试将导航属性与外键一起使用,但未填充外键,但看起来答案在您的示例中。谢谢!
      • 如果实际对象存在属性,您是否需要定义 ID 属性?那么在 User 对象中你需要定义 RoleId 还是只是为了方便?
      • @DDiVita: 是否在User 中定义RoleId 是一个很大的区别。我推荐这个答案来看看有什么不同:stackoverflow.com/questions/5281974/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      相关资源
      最近更新 更多