实体框架标识列问题

时间:2013-12-06 13:08:30

标签: c# asp.net-mvc entity-framework

目前我的EF存在问题。 我有一个现有的数据库,并且有一个名为Profiles的自定义用户表。 用户在下面(我已经删除了大部分属性以便于阅读)。

public partial class Profile : IdentityUser
{
    public Profile()
    {
        this.Assets = new List<Asset>();

        // ...
    }

    public string CompanyId { get; set; }

    // ...

    public virtual Company Company { get; set; }
    public virtual ICollection<Asset> Assets { get; set; }

    // ...

}

和我的DbContext看起来像这样(简化):

public partial class SkipstoneContext : IdentityDbContext<Profile>
{
    static SkipstoneContext()
    {
        Database.SetInitializer<SkipstoneContext>(null);
    }

    public SkipstoneContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<Asset> Assets { get; set; }

    // ...

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // ...

        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
        modelBuilder.Entity<UserSecret>().HasKey<string>(r => r.UserName);
    }
}

我有一个看起来像这样的课程:

    public Company()
    {
        this.Assets = new List<Asset>();
    }

    public string Id { get; set; }
    public string Name { get; set; }
    public string CreatedBy { get; set; }
    public string ModifiedBy { get; set; }
    public System.DateTime DateCreated { get; set; }
    public Nullable<System.DateTime> DateModified { get; set; }
    public bool Deleted { get; set; }

    public virtual Profile CreatedByProfile { get; set; }
    public virtual ICollection<Asset> Assets { get; set; }
}

问题是当我运行我的代码时,我收到一条错误说明:

  

无效的列名称'CreatedByProfile_Id'。

我需要告诉系统我的自定义用户的 ID 列只是 ID 。 我有一个映射文件:

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

        // Properties
        this.Property(t => t.Id)
            .IsRequired()
            .HasMaxLength(128);

        this.Property(t => t.CompanyId)
            .IsRequired()
            .HasMaxLength(128);

        this.Property(t => t.CreatedBy)
            .IsRequired();

        this.Property(t => t.Title)
            .IsRequired();

        this.Property(t => t.Forename)
            .IsRequired();

        this.Property(t => t.Surname)
            .IsRequired();

        this.Property(t => t.Email)
            .IsRequired();

        this.Property(t => t.CredentialId)
            .IsRequired();

        // Table & Column Mappings
        this.ToTable("Profiles");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.CompanyId).HasColumnName("CompanyId");
        this.Property(t => t.CreatedBy).HasColumnName("CreatedBy");
        this.Property(t => t.ModifiedBy).HasColumnName("ModifiedBy");
        this.Property(t => t.DateCreated).HasColumnName("DateCreated");
        this.Property(t => t.DateModified).HasColumnName("DateModified");
        this.Property(t => t.LastLoginDate).HasColumnName("LastLoginDate");
        this.Property(t => t.Title).HasColumnName("Title");
        this.Property(t => t.Forename).HasColumnName("Forename");
        this.Property(t => t.Surname).HasColumnName("Surname");
        this.Property(t => t.Email).HasColumnName("Email");
        this.Property(t => t.JobTitle).HasColumnName("JobTitle");
        this.Property(t => t.Telephone).HasColumnName("Telephone");
        this.Property(t => t.Mobile).HasColumnName("Mobile");
        this.Property(t => t.Photo).HasColumnName("Photo");
        this.Property(t => t.LinkedIn).HasColumnName("LinkedIn");
        this.Property(t => t.Twitter).HasColumnName("Twitter");
        this.Property(t => t.Facebook).HasColumnName("Facebook");
        this.Property(t => t.Google).HasColumnName("Google");
        this.Property(t => t.Bio).HasColumnName("Bio");
        this.Property(t => t.CompanyName).HasColumnName("CompanyName");
        this.Property(t => t.CredentialId).HasColumnName("CredentialId");
        this.Property(t => t.IsLockedOut).HasColumnName("IsLockedOut");
        this.Property(t => t.IsApproved).HasColumnName("IsApproved");
        this.Property(t => t.CanEditOwn).HasColumnName("CanEditOwn");
        this.Property(t => t.CanEdit).HasColumnName("CanEdit");
        this.Property(t => t.CanDownload).HasColumnName("CanDownload");
        this.Property(t => t.RequiresApproval).HasColumnName("RequiresApproval");
        this.Property(t => t.CanApprove).HasColumnName("CanApprove");
        this.Property(t => t.CanSync).HasColumnName("CanSync");
        this.Property(t => t.AgreedTerms).HasColumnName("AgreedTerms");
        this.Property(t => t.Deleted).HasColumnName("Deleted");
        this.Property(t => t.UserName).HasColumnName("UserName");

        // Relationships
        this.HasRequired(t => t.Company)
            .WithMany(t => t.Users)
            .HasForeignKey(d => d.CompanyId);

    }
}

但是如果我将它添加到我的DbContext类中,我会收到一条错误说明:

  

已添加“Models.Profile”类型的配置。至   引用现有配置使用Entity()或   ComplexType()方法。

我认为这很容易解决,所以有人能指出我正确的方向吗?

干杯,

/ r3plica

1 个答案:

答案 0 :(得分:2)

问题出在您的Company班级配置上。 尝试:

public class CompanyMap : EntityTypeConfiguration<Company>
{
  public CompanyMap()
  {
    // Add this
    this.HasRequired(x => x.CreatedByProfile).WithMany().Map(
      x => x.MapKey("CreatedByProfileId"));

    // CreatedByProfileId is the FK column in the Company table that points
    // to the Profile table. This is my made up name for the column since
    // I don't know the real name in your database.
  }
}

如果您没有Company的配置类,则需要使用OnModelCreating方法:

modelBuilder.Entity<Company>().HasRequired(x => x.CreatedByProfile).WithMany().Map(
  x => x.MapKey("CreatedByProfileId"));

<强>更新
由于您已经在班上拥有该物业。

modelBuilder.Entity<Company>().HasRequired(
  x => x.CreatedByProfile).WithMany().HasForeignKey(x => x.CreatedBy);