EntityType'CustomUserLogin'没有定义键。定义此EntityType的键

时间:2015-08-02 09:51:19

标签: entity-framework webforms asp.net-identity

我刚刚在Web窗体中开始了一个新项目,因为我认为它比学习MVC更快,但我有多么错!我正在努力实现项目的身份方面。我在这里点了这个链接:http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity

这一切都编译好并且有效(我的ID列现在是一个整数)。

所以我为'Property'上下文创建了一个新类:

using System.Collections.Generic;
using System.Data.Entity;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Web.Security;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace MyApp.Models
{
public class PropertyContext : DbContext
{
    public PropertyContext()
        : base("DefaultConnection")
    {
    }
    public DbSet<Property> Property { get; set; }      

}

public class Property
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), ScaffoldColumn(false)]
    public int PropertyID { get; set; }

    //[Required, StringLength(128)]
    //public string OwnerID { get; set; }

  //  [ForeignKey("OwnerID")]
    //public virtual ApplicationUser ApplicationUser { get; set; }

    //[ForeignKey("OwnerID")]
    //public int OwnerID { get; set; }
   // public virtual ApplicationUser ApplicationUser { get; set; }


    public int OwnerID { get; set; }
    [ForeignKey("OwnerID")]
    public virtual ApplicationUser ID { get; set; }


    [Required, StringLength(255), Display(Name = "Address 1")]
    public string Address1 { get; set; }
    [Required, StringLength(255), Display(Name = "Address 2")]
    public string Address2 { get; set; }
    [Required, StringLength(255), Display(Name = "Address 3")]
    public string Address3 { get; set; }
    [Required, StringLength(255), Display(Name = "Town/City")]
    public string Settlement { get; set; }
    [Required, StringLength(255), Display(Name = "County")]
    public string County { get; set; }
    [Required, StringLength(255), Display(Name = "Post Code")]
    public string PostCode { get; set; }
    [Required, StringLength(255), Display(Name = "Country")]
    public string Country { get; set; }

}

}

因此,我正在尝试将名为“OwnerID”的新int列作为“AspNetUser”表的ID列中的外键。当我构建我的项目时,它构建良好。但是,当我尝试添加迁移时,我得到了这个:

MyApp.Models.CustomUserLogin: : EntityType 'CustomUserLogin' has no key defined. Define the key for this EntityType.
MyApp.Models.CustomUserRole: : EntityType 'CustomUserRole' has no key defined. Define the key for this EntityType.
CustomUserLogins: EntityType: EntitySet 'CustomUserLogins' is based on type 'CustomUserLogin' that has no keys defined.
CustomUserRoles: EntityType: EntitySet 'CustomUserRoles' is based on type 'CustomUserRole' that has no keys defined.

所以在我的ApplicationDbContext中我添加了这个:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ApplicationUser>().ToTable("Users").HasKey<int>(u => u.Id);
        modelBuilder.Entity<CustomUserRole>().ToTable("UserRoles").HasKey(ur => new { ur.RoleId, ur.UserId });
        modelBuilder.Entity<CustomUserLogin>().ToTable("UserLogins").HasKey<int>(ul => ul.UserId);
        modelBuilder.Entity<CustomUserClaim>().ToTable("UserClaims").HasKey<int>(uc => uc.Id);
        modelBuilder.Entity<CustomRole>().ToTable("Roles").HasKey<int>(r => r.Id);



    }

甚至这个

base.OnModelCreating(modelBuilder);
modelBuilder.Entity<CustomUserLogin>().HasKey(cul => cul.UserId);
modelBuilder.Entity<CustomUserRole>().HasKey(cur => cur.UserId);

但它仍然无效。说实话,我在黑暗中刺伤整整一个周末。我无法相信在这个Entity Framework环境中创建外键这么简单的事情。

任何人都可以给我指针吗?感谢。

2 个答案:

答案 0 :(得分:0)

我完全和你一样。

  • 向我的MVC Web应用程序添加了标识
  • 将主键更改为INT而非GUID
  • 向ApplicationUser添加了EmployeeProfile和CustomerProfile

我没有覆盖OnModelCreating,因为我对数据库表进行了自己的更改以支持我需要的内容。

enter image description here

enter image description here

在IdentityModels.cs

中为ApplicationUser类添加了两个新属性
 public virtual CustomerProfile CustomerProfile { get; set; }

 public virtual EmployeeProfile EmployeeProfile { get; set; }

我创建了CustomerProfile和EmployeeProfile类。请注意每个类的Id属性上的数据注释。 Id属性是ForeignKey属性,用于将虚拟User属性引导回ApplicationUser。

 public class EmployeeProfile
{       
    [Key, ForeignKey("User")]
    public int Id { get; set; }
    public DateTime HireDate { get; set; }
    public DateTime BirthDate{ get; set; }        

    public virtual ApplicationUser User { get; set; }

}

public class CustomerProfile
{
    [Key, ForeignKey("User")]
    public int Id { get; set; }
    public CustomerType CustomerType { get; set; }
    public string Company { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string WorkPhone { get; set; }       
    public string CustomerId { get; set; }     
    public string AccountType { get; set; } 

    public virtual ApplicationUser User { get; set; }
}

在我的ApplicationDbContext类中,我添加了以下代码行:

public DbSet<CustomerProfile> CustomerProfile { get; set; }
public DbSet<EmployeeProfile> EmployeeProfile { get; set; }

一切都按预期工作。我登录并在我的视图中看到了客户和员工信息。

当我在EmployeeProfile类中向部门添加多对多关系时,我开始看到您收到的错误“ EntityType'CustomUserLogin'没有定义键。定义此EntityType的键”类。在我的场景中,员工可以属于一个或多个部门,一个部门可以拥有许多员工。我的数据库更改很好,甚至像您所经历的那样编译了类,但是一旦我运行该项目,网站就会出错。

退出我的更改解决了这个问题。我的课程中的某些东西放弃了模型的验证,所以我需要以不同的方式处理问题。就像在我的情况下,你的poco类可能有问题。

删除您的“Property”类和引用。使项目与基本要素一起使用。一旦你运行它,只需使用Id属性添加Property类。

如果您仍需要帮助,请告诉我。

答案 1 :(得分:0)

base.OnModelCreating放在底部:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    modelBuilder.Entity<ApplicationUser>().ToTable("Users").HasKey<int>(u => u.Id);
    modelBuilder.Entity<CustomUserRole>().ToTable("UserRoles").HasKey(ur => new { ur.RoleId, ur.UserId });
    modelBuilder.Entity<CustomUserLogin>().ToTable("UserLogins").HasKey<int>(ul => ul.UserId);
    modelBuilder.Entity<CustomUserClaim>().ToTable("UserClaims").HasKey<int>(uc => uc.Id);
    modelBuilder.Entity<CustomRole>().ToTable("Roles").HasKey<int>(r => r.Id);

    base.OnModelCreating(modelBuilder);
}