简体   繁体   English

在EF中映射实体时出错

[英]Error with mapping entities in EF

I am trying to implement Asp.net Identity 2.0 with DB first. 我想首先用DB实现Asp.net Identity 2.0。

I have imported my model.edmx into the project. 我已将model.edmx导入到项目中。 It contains all the tables I need with the correct information and structure. 它包含了我需要的所有表格以及正确的信息和结构。

In the database there is a table called 'FSKUsers' I have edited this to contain the needed fields of the AspNetUsers which is the default table for Identity 2.0 在数据库中有一个名为'FSKUsers'的表我编辑了这个表,包含AspNetUsers的必需字段,这是Identity 2.0的默认表

So in my Identity DB Context I have mapped my FskUser class (which is a high level user for Identity sake) 所以在我的身份数据库上下文中,我已经映射了我的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();
    }
}

So basically I want to map the class FskUser to the Data Base table called FSKUser which is also contained in my .edmx model. 所以基本上我想将类FskUser映射到名为FSKUser的数据库表,该表也包含在我的.edmx模型中。

When I run the website I get the following error. 当我运行网站时,我收到以下错误。

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

My two POCO classes: 我的两个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; }
}

The one I use in my Identity Config 我在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;
    }
}

When you use Database first approach with edmx file OnModelCreating method is never called. 当您使用数据库第一种方法与edmx文件时,永远不会调用OnModelCreating方法。 You may check that with debugger. 您可以使用调试器进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM