Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with mapping entities in EF

I am trying to implement Asp.net Identity 2.0 with DB first.

I have imported my model.edmx into the project. 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

So in my Identity DB Context I have mapped my FskUser class (which is a high level user for Identity sake)

 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.

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:

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

 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;
    }
}
like image 578
Zapnologica Avatar asked Mar 02 '26 23:03

Zapnologica


1 Answers

When you use Database first approach with edmx file OnModelCreating method is never called. You may check that with debugger.

like image 83
mr100 Avatar answered Mar 05 '26 13:03

mr100



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!