助けてください。私はシステムのさまざまな部分をやり直して、いろいろな解決策を試して一日を失ってしまったが、アイデアはなかった。USERSとAspNetUserを同時に保存するための支援
私は同じアプリケーションの解決策を持っています。これらのアプリケーションの1つでは、Entity FrameworkのDataBase Firstメソッドでコンテキストを作成しました。注:私は既にデータベースに、作成スクリプト、IDテーブルを含めていました。
私の解決策の別のレイヤーでは、ユーザーコントロールなしで純粋なMVCアプリケーションを追加しました。そこで、nuget、Entity FrameworkとIdentityを追加しました。
そして最後に、私は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(string Id)を持っていて、データベース内の多くの関係をやり直さなければならないため、うまくいきませんでした。そこで私は2つのテーブルを保管しました。 IdAspNetUserをUSERSテーブルに追加し、1対1の関係になっています。
ApplpicationUserと連携してユーザーを登録するビューを作成しましたが、正常に機能していました。しかし、1つのuserManager.addを実行してから1つのContextPerformance.USUARIOS.Addを実行していたので、それは奇妙だと思っていました。それらの間の関係が1対1であるため、私はApplicationUserに仮想プロパティUSERS USERS {getを追加するという素晴らしいアイデアを持っていました。私が間違いやラップタイムを持ち始めた時です。その後、私は[NotMapped]としてApplicationUser内のユーザーの仮想プロパティをマークすると
System.Data.Entity.ModelConfiguration.ModelValidationException "One or more validation errors were detected during model generation: GPSd.Web.MVC.IdentityContext.AspNetUserLogins:: EntityType 'AspNetUserLogins' has no key defined. Set the key for this EntityType. AspNetUserLogins: EntityType: EntitySet 'AspNetUserLogins' is based on type 'AspNetUserLogins' that has no keys defined."
、アプリケーションが再び動作しますが、:
私は試してみてください。
private UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new GPSdEntitiesIdentity()));
List<ApplicationUser> ApplicationUsers = new List<ApplicationUser>();
ApplicationUsers = userManager.Users.ToList();
私は、次のエラーを取得していますAspNetIdとUSERを個別に保存する必要があります
私はこのような単純なことをしようとしています:
あなたのGPSdEntitiesIdentity
の追加
ApplicationUser identityUser = new ApplicationUser
{
UserName = model.LOGIN,
LOGIN = model.LOGIN,
SENHA = model.SENHA,
USERS = model.USERS
};
IdentityResult resultado = userManager.Create(identityUser, model.SENHA);
あなたは、2つの独立したコンテキストを持っていますか? 1つは 'AspNetUserLogins'を含み、もう1つは' Users'を含んでいますか?もしそうなら、 'Users'を含む他のコンテキストの' AspNetUserLogins'にマッピングを追加する必要があります。 – Monah
@Monahはいは、2つのコンテクスト1の一般的なもので、アイデンティティのものです。次に、IdentityContextにaspnetTablesとUSERSを追加するためにOnModelCreatingをオーバーライドする必要がありますか?はい...それを作る...ありがとう私はこれをやってみてください。 – AlamEstudent
'ModelBuilder.Entity()。HasKey(t => t.Id);'を追加すれば十分です。他のマッピングは最初のコンテキストで既に定義されているので不要です。 –
Monah