实体框架映射错误

时间:2015-05-30 23:58:35

标签: c# .net entity-framework entity-framework-6

我将以下实体定义为:

public partial class User
{
    public User()
    {
        this.UserClaims = new List<UserClaim>();
        this.UserRoles = new List<UserRole>();
        this.UserLogins = new List<UserLogin>();
    }

    public long Id { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    public Nullable<byte> Gender { get; set; }
    public long AddressId { get; set; }
    public string Email { get; set; }
    public bool EmailConfirmed { get; set; }
    public string PasswordHash { get; set; }
    public string SecurityStamp { get; set; }
    public string PhoneNumber { get; set; }
    public bool PhoneNumberConfirmed { get; set; }
    public bool TwoFactorEnabled { get; set; }
    public Nullable<DateTime> LockoutEndDateUtc { get; set; }
    public bool LockoutEnabled { get; set; }
    public int AccessFailedCount { get; set; }
    public virtual Address Address { get; set; }
    public virtual ICollection<UserClaim> UserClaims { get; set; }
    public virtual ICollection<UserRole> UserRoles { get; set; }
    public virtual ICollection<UserLogin> UserLogins { get; set; }
}

public partial class UserClaim    {
    public int Id { get; set; }
    public string ClaimType { get; set; }
    public string ClaimValue { get; set; }
    public long UserId { get; set; }
    public virtual User User { get; set; }
}

映射如下:

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.UserName)
            .IsRequired()
            .HasMaxLength(50);

        this.Property(t => t.FirstName)
            .HasMaxLength(50);

        this.Property(t => t.LastName)
            .HasMaxLength(50);

        this.Property(t => t.MiddleName)
            .HasMaxLength(50);

        this.Property(t => t.Email)
            .HasMaxLength(100);

        this.Property(t => t.PasswordHash)
            .HasMaxLength(100);

        this.Property(t => t.SecurityStamp)
            .HasMaxLength(100);

        this.Property(t => t.PhoneNumber)
            .HasMaxLength(25);

        // Table & Column Mappings
        this.ToTable("Users");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.UserName).HasColumnName("UserName");
        this.Property(t => t.FirstName).HasColumnName("FirstName");
        this.Property(t => t.LastName).HasColumnName("LastName");
        this.Property(t => t.MiddleName).HasColumnName("MiddleName");
        this.Property(t => t.Gender).HasColumnName("Gender");
        this.Property(t => t.AddressId).HasColumnName("AddressId");
        this.Property(t => t.Email).HasColumnName("Email");
        this.Property(t => t.EmailConfirmed).HasColumnName("EmailConfirmed");
        this.Property(t => t.PasswordHash).HasColumnName("PasswordHash");
        this.Property(t => t.SecurityStamp).HasColumnName("SecurityStamp");
        this.Property(t => t.PhoneNumber).HasColumnName("PhoneNumber");
        this.Property(t => t.PhoneNumberConfirmed).HasColumnName("PhoneNumberConfirmed");
        this.Property(t => t.TwoFactorEnabled).HasColumnName("TwoFactorEnabled");
        this.Property(t => t.LockoutEndDateUtc).HasColumnName("LockoutEndDateUtc");
        this.Property(t => t.LockoutEnabled).HasColumnName("LockoutEnabled");
        this.Property(t => t.AccessFailedCount).HasColumnName("AccessFailedCount");

        // Relationships
        this.HasMany(t => t.UserRoles)
            .WithMany(t => t.Users)
            .Map(m =>
                {
                    m.ToTable("UserUserRoles");
                    m.MapLeftKey("UserId");
                    m.MapRightKey("RoleId");
                });

        this.HasRequired(t => t.Address)
            .WithMany(t => t.Users)
            .HasForeignKey(d => d.AddressId);

        this.HasMany(t => t.UserClaims)
        .WithOptional(t => t.User)
        .HasForeignKey(d => d.UserId);
    }
}

public class UserClaimMap : EntityTypeConfiguration<UserClaim>
{
    public UserClaimMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        // Table & Column Mappings
        this.ToTable("UserClaims");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.ClaimType).HasColumnName("ClaimType");
        this.Property(t => t.ClaimValue).HasColumnName("ClaimValue");
        this.Property(t => t.UserId).HasColumnName("UserId");

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

    }
}

UserClaim表在“UserId”列上有外键。数据库表如下所示。 enter image description here

在运行应用程序时,我收到以下错误。 在模型生成期间检测到一个或多个验证错误:

UserClaim_User_Source :: Multiplicity在关系'UserClaim_User'中的角色'UserClaim_User_Source'中无效。由于“从属角色”是指关键属性,因此从属角色的多重性的上限必须为“1”。 UserClaim_User_Target_UserClaim_User_Source ::参照约束的从属角色中的所有属性的类型必须与主体角色中的相应属性类型相同。实体'UserClaim'上的属性'Id'类型与参照约束'UserClaim_User'中实体'User'上的属性'Id'不匹配。

我尝试更改映射但无法解决此错误。有人可以帮我解决这个错误吗?

0 个答案:

没有答案