在EF中映射实体时出错

时间:2015-04-07 11:54:46

标签: c# asp.net entity-framework asp.net-identity-2 ef-database-first

我首先尝试使用DB实现Asp.net Identity 2.0。

我已将model.edmx导入项目中。它包含了我需要的所有表格以及正确的信息和结构。

在数据库中有一个名为'FSKUsers'的表我已对其进行了编辑,以包含AspNetUsers所需的字段,这是Identity 2.0的默认表

所以在我的身份数据库上下文中,我已经映射了我的FskUser类(为了身份而这是一个高级用户)

 public class IdentityDbContext : IdentityDbContext<FskUser, FskRole, int, FskUserLogin, FskUserRole, FskUserClaim>
{
    public IdentityDbContext()
        : base("FSK_FskNetworksEntities")
    {
    }

    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        var userEntity = modelBuilder.Entity<FskUser>();

        userEntity.ToTable("FSKUsers", "dbo");
        userEntity.Property(p => p.Id).HasColumnName("FSKUserId");
        userEntity.Property(p => p.PasswordHash).HasColumnName("Password");
    }

    public static IdentityDbContext Create()
    {
        return new IdentityDbContext();
    }
}

所以基本上我想将类FskUser映射到名为FSKUser的数据库表,该表也包含在我的.edmx模型中。

当我运行网站时,我收到以下错误。

The entity type FskUser is not part of the model for the current context

我的两个POCO课程:

The one from my edmx model:

public partial class FSKUser
{
    public FSKUser()
    {
        this.AspNetUserClaims = new HashSet<AspNetUserClaim>();
        this.AspNetUserLogins = new HashSet<AspNetUserLogin>();
        this.FSKDevices = new HashSet<FSKDevice>();
        this.FSKEventLogs = new HashSet<FSKEventLog>();
        this.FSKReports = new HashSet<FSKReport>();
        this.FSKTransactions = new HashSet<FSKTransaction>();
        this.FSKTriggers = new HashSet<FSKTrigger>();
        this.UdlDownloads = new HashSet<UdlDownload>();
        this.AspNetRoles = new HashSet<AspNetRole>();
        this.FSKCompanies = new HashSet<FSKCompany>();
    }

    public int FSKUserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public bool EmailConfirmed { get; set; }
    public string PhoneNumber { get; set; }
    public bool PhoneNumberConfirmed { get; set; }
    public string Password { get; set; }
    public string SecurityStamp { get; set; }
    public bool TwoFactorEnabled { get; set; }
    public Nullable<System.DateTime> LockoutEndDateUtc { get; set; }
    public bool LockoutEnabled { get; set; }
    public int AccessFailedCount { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public byte FSKAccessLevelId { get; set; }
    public string AddressStreet1 { get; set; }
    public string AddressStreet2 { get; set; }
    public string AddressStreet3 { get; set; }
    public string AddressPostCode { get; set; }
    public Nullable<int> CreatorId { get; set; }
    public Nullable<System.DateTime> CreateDate { get; set; }
    public string ConfirmationToken { get; set; }
    public Nullable<bool> IsConfirmed { get; set; }
    public Nullable<System.DateTime> LastPasswordFailureDate { get; set; }
    public Nullable<int> PasswordFailuresSinceLastSuccess { get; set; }
    public Nullable<System.DateTime> PasswordChangedDate { get; set; }
    public string PasswordVerificationToken { get; set; }
    public string PasswordVerificationTokenExpirationDate { get; set; }
    public bool IsDeleted { get; set; }
    public Nullable<int> CostCentreId { get; set; }
    public Nullable<int> AdminPasswordResetUserId { get; set; }
    public Nullable<System.DateTime> PreviousLogInDate { get; set; }
    public System.Guid msrepl_tran_version { get; set; }

    public virtual ICollection<AspNetUserClaim> AspNetUserClaims { get; set; }
    public virtual ICollection<AspNetUserLogin> AspNetUserLogins { get; set; }
    public virtual ICollection<FSKDevice> FSKDevices { get; set; }
    public virtual ICollection<FSKEventLog> FSKEventLogs { get; set; }
    public virtual ICollection<FSKReport> FSKReports { get; set; }
    public virtual ICollection<FSKTransaction> FSKTransactions { get; set; }
    public virtual ICollection<FSKTrigger> FSKTriggers { get; set; }
    public virtual ICollection<UdlDownload> UdlDownloads { get; set; }
    public virtual ICollection<AspNetRole> AspNetRoles { get; set; }
    public virtual ICollection<FSKCompany> FSKCompanies { get; set; }
}

我在Identity Config中使用的那个

 public class FskUser : IdentityUser<int, FskUserLogin, FskUserRole, FskUserClaim>
{
    [Display(Name = "First Name")]
    [Required(ErrorMessage = "First Name is Required.")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    [Required(ErrorMessage = "Last Name is Required.")]
    public string LastName { get; set; }        

    [MaxLength(20)]
    [Display(Name = "Cell Number")]
    [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Entered phone format is not valid.")]
    [StringLength(10, ErrorMessage = "The {0} must be 10 numbers long.", MinimumLength = 10)]
    public override string PhoneNumber { get; set; }



    [Display(Name = "Access Level")]
    public byte? FSKAccessLevelId { get; set; }

    [Display(Name = "Street Address 1")]
    public string AddressStreet1 { get; set; }

    [Display(Name = "Street Address 2")]
    public string AddressStreet2 { get; set; }

    [Display(Name = "Street Address 3")]
    public string AddressStreet3 { get; set; }

    [Display(Name = "Postal Code")]
    public string AddressPostCode { get; set; }

    [Display(Name = "Previous Login")]
    public Nullable<DateTime> PreviousLogInDate { get; set; }

    [Display(Name = "Account Confirmed")]
    public Nullable<bool> IsConfirmed { get; set; }

    [Display(Name = "Last Password Failier")]
    public Nullable<DateTime> LastPasswordFailureDate { get; set; }

    [Display(Name = "Password Last Changed")]
    public Nullable<DateTime> PasswordChangedDate { get; set; }


    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<FskUser, int> manager)
    {
        //TODO: add option for web and api (to create different auth types

        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

1 个答案:

答案 0 :(得分:2)

当您使用数据库第一种方法与edmx文件时,永远不会调用OnModelCreating方法。您可以使用调试器进行检查。