【问题标题】:MVC4 Code first approach, how to fix the following?MVC4代码优先方法,如何解决以下问题?
【发布时间】:2013-12-01 14:45:57
【问题描述】:

我收到以下错误 在模型生成期间检测到一个或多个验证错误:

\tSystem.Data.Entity.Edm.EdmAssociationEnd: : 多重性在关系“UserRoles_Roles”中的角色“UserRoles_Roles_Source”中无效。因为从属角色是指关键属性,所以从属角色的多重性的上限必须是'1'。

我的实体和关联的映射定义如下,

public class UserProfile
    {
        public UserProfile()
        {
           UserUserRoles = new List<UserRoles>();       
        }         

        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }      
        public virtual ICollection<UserRoles> UserUserRoles { get; set; } 
    }

 public class Roles
    {
        public Roles()
        {
            RoleUserRoles = new List<UserRoles>();
        }

        public int RoleId { get; set; }
        public string RoleName { get; set; }


        public virtual ICollection<UserRoles> RoleUserRoles { get; set; }
    }

  public class UserRoles
    {
        public int UserId { get; set; }
        public int RoleId { get; set; }


        public virtual UserProfile User { get; set; }
        public virtual Roles Roles { get; set; }

    }

//Mappings

  public UserProfileMap()
        {
            // Primary Key
            HasKey(t => t.UserId);

            // Properties
            Property(t => t.UserName)
                .HasMaxLength(56);     

            // Table & Column Mappings
            ToTable("UserProfile");
            Property(t => t.UserId).HasColumnName("UserId");
            Property(t => t.UserName).HasColumnName("UserName");
        }

public class RolesMap : EntityTypeConfiguration<Roles>
    {
        public RolesMap()
        {
            // Primary Key
            HasKey(t => t.RoleId);

            // Properties
            Property(t => t.RoleName)
                .HasMaxLength(256);       

            // Table & Column Mappings
            ToTable("webpages_Roles");
            Property(t => t.RoleId).HasColumnName("RoleId");
            Property(t => t.RoleName).HasColumnName("RoleName");
        }
    }

public class UserRolesMap : EntityTypeConfiguration<UserRoles>
    {
        public UserRolesMap()
        {
            // Primary Key
            HasKey(t => t.UserId);
            HasKey(t => t.RoleId);          

            // Table & Column Mappings
            ToTable("webpages_UsersInRoles");
            Property(t => t.UserId).HasColumnName("UserId");
            Property(t => t.RoleId).HasColumnName("RoleId");


            // Relationships              
            HasRequired(t => t.User)
                .WithMany(t => t.UserUserRoles)
                .HasForeignKey(d => d.UserId);

            HasRequired(t => t.Roles)
                .WithMany(t => t.RoleUserRoles)
                .HasForeignKey(d => d.RoleId);
        }
    }

我做错了什么?

【问题讨论】:

  • 我不确定它是否有区别,但组合主键应构造为HasKey(t =&gt; new { t.UserId, t.RoleId });
  • Jeroen - 谢谢伙计,这就是问题所在
  • 我很乐意提供帮助。我会将其作为答案发布,以便您将其标记为已解决。

标签: entity-framework asp.net-mvc-4 linq-to-entities


【解决方案1】:

您似乎想在模型之间创建多对多关系。在这种情况下你不需要类UserRoles,它会在使用这个场景时由EF生成:

public class User
{
  // other properties

  public virtual IList<Role> Roles {get;set;}
}

public class Role
{
  // other properties

  public virtual IList<User> Users {get;set;}
}

但是,如果您确实希望 UserRoles 模型存储其他信息,您需要手动进行映射,例如 DbContext 类的 OnModelCreating() 函数。

【讨论】:

    【解决方案2】:

    根据 cmets:

    组合主键应构造为HasKey(t =&gt; new { t.UserId, t.RoleId });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 2023-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多