簡體   English   中英

協助同時保存USERS和AspNetUser

[英]Assistance to save USERS and AspNetUser at the same time

請幫助。 我一整天都在重做系統的各個部分,嘗試了許多不同的解決方案,但最終卻一無所獲。

我有一個具有相同應用程序的解決方案。 在這些應用程序之一(嘗試隔離數據持久性)中,我使用Entity Framework中的DataBase First方法創建了上下文。 注意:我已經使用創建腳本將身份表包含在數據庫中。

在解決方案的另一層中,我添加了一個沒有用戶控制的純MVC應用程序。 所以我添加了nuget,實體框架和身份。

最后,我創建了ApplicationUser,IdentityDbContext和ApplicationRoleManager:

public partial class ApplicationUser : IdentityUser
{
    [Display(Name = "Usuario", Description = "Identificação para Login")]
    [Required(ErrorMessage = "000027 - O Login não pode ser vazio ou nulo.")]
    [MaxLength(30, ErrorMessage = "000028 - O Login pode ter no máximo 30 caracteres.")]
    [DuplicadoNaBase("AspNetUsersDao", "SelecionarPorLogin", ErrorMessage = "000031 - O Login informado já existe na base de dados.")]
    public string LOGIN { get; set; }

    [NotMapped]
    [Display(Name = "Senha", Description = "Senha para Login")]
    [Required(ErrorMessage = "000029 - A senha não pode ser vazia ou nula.")]
    [MaxLength(30, ErrorMessage = "000030 - A senha pode ter no máximo 30 caracteres.")]
    [DataType(DataType.Password)]
    public string SENHA { get; set; }

    public virtual USUARIOS USUARIOS { get; set;}

    [NotMapped]
    public string Perfil { get; set; }
}

public class GPSdEntitiesIdentity :  IdentityDbContext<ApplicationUser>
{
    public GPSdEntitiesIdentity() : base("IdentityConnection")
    {
    }
}
public class ApplicationRoleManager : RoleManager<IdentityRole, string>
{
    public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
        : base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        return new ApplicationRoleManager(new RoleStore<IdentityRole>(new GPSdEntities()));
    }
}

好吧,基本上我的問題發生了,因為我已經有一個帶有USER表的數據庫。 最初,我嘗試將USER表與AspNetUser合並。 但這不起作用,因為AspNetUser具有該GUID(字符串ID),並且我將不得不重做數據庫中的許多關系。 所以我保留了兩個桌子。 我在我的USERS表中添加了一個IdAspNetUser,他們之間存在1對1的關系。

我制作了一個與ApplpicationUser一起使用的View來注冊用戶,並且工作正常。 但是我認為這很奇怪,因為我當時在做1個userManager.add然后是1個ContextPerformance.USUARIOS.Add。 由於它們之間的關系是1對1,所以我有一個絕妙的主意,在ApplicationUser中添加一個虛擬屬性USERS USERS {get; set},那是我開始出現錯誤和圈速的時候。

當我嘗試:

private UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new GPSdEntitiesIdentity()));
List<ApplicationUser> ApplicationUsers = new List<ApplicationUser>();
ApplicationUsers = userManager.Users.ToList();

我收到以下錯誤:

System.Data.Entity.ModelConfiguration.ModelValidationException“在模型生成期間檢測到一個或多個驗證錯誤:GPSd.Web.MVC.IdentityContext.AspNetUserLogins :: EntityType'AspNetUserLogins'沒有定義鍵。為此EntityType設置鍵。AspNetUserLogins: EntityType:EntitySet'AspNetUserLogins'基於類型'AspNetUserLogins',其中未定義任何鍵。

如果我在ApplicationUser中將USERS Virtual屬性標記為[NotMapped],則該應用程序將再次運行,但是我必須分別保存AspNetId和USER

我正在嘗試做這樣的簡單事情:

ApplicationUser identityUser = new ApplicationUser
{
   UserName = model.LOGIN,
   LOGIN = model.LOGIN,
   SENHA = model.SENHA,
   USERS = model.USERS
};
IdentityResult resultado = userManager.Create(identityUser, model.SENHA);

在您的GPSdEntitiesIdentity添加:

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

      modelBuilder.Entity<IdentityUserLogin>()
            .HasKey(l => new { l.LoginProvider, l.ProviderKey, l.UserId })
            .ToTable("AspNetUserLogins");// Database Table Name   

    }

暫無
暫無

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

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