簡體   English   中英

在EF中映射實體時出錯

[英]Error with mapping entities in EF

我想首先用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;
    }
}

當您使用數據庫第一種方法與edmx文件時,永遠不會調用OnModelCreating方法。 您可以使用調試器進行檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM