Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 帮助同时保存用户和AspNetUser_C#_.net_Asp.net Mvc_Entity Framework_Asp.net Identity - Fatal编程技术网

C# 帮助同时保存用户和AspNetUser

C# 帮助同时保存用户和AspNetUser,c#,.net,asp.net-mvc,entity-framework,asp.net-identity,C#,.net,Asp.net Mvc,Entity Framework,Asp.net Identity,请帮忙。我花了一整天的时间重做系统的各个部分,尝试了很多不同的解决方案,但最终没有任何想法 我有一个具有相同应用程序的解决方案。在其中一个应用程序中(我尝试隔离数据持久性),我使用实体框架中的数据库优先方法创建了上下文。注意:我已经在数据库中包含了一个创建脚本,即标识表 在我的解决方案的另一层中,我添加了一个没有用户控制的纯MVC应用程序。所以我添加了nuget、实体框架和标识 最后,我创建了ApplicationUser、IdentityDbContext和ApplicationRoleMan

请帮忙。我花了一整天的时间重做系统的各个部分,尝试了很多不同的解决方案,但最终没有任何想法

我有一个具有相同应用程序的解决方案。在其中一个应用程序中(我尝试隔离数据持久性),我使用实体框架中的数据库优先方法创建了上下文。注意:我已经在数据库中包含了一个创建脚本,即标识表

在我的解决方案的另一层中,我添加了一个没有用户控制的纯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()));
    }
}

在您的
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   

    }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity()
.HasKey(l=>new{l.LoginProvider,l.ProviderKey,l.UserId})
.ToTable(“AspNetUserLogins”);//数据库表名
}

在您的
GPS实体标识中添加:

 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   

    }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity()
.HasKey(l=>new{l.LoginProvider,l.ProviderKey,l.UserId})
.ToTable(“AspNetUserLogins”);//数据库表名
}

您有两种不同的上下文吗?一个包含
AspNetUserLogins
,另一个包含
Users
?,如果是这样,您需要在另一个包含
Users
的上下文中添加映射到
AspNetUserLogins
,然后您的工作应该正常。@Monah Yes是2 contexto 1 general,其他是to Identity。然后我需要重写OnModelCreating,在IdentityContext中添加aspnetTables和用户?对这很有意义。。。感谢我尝试这么做。添加
modelBuilder.Entity().HasKey(t=>t.Id)就足够了不需要另一个映射,因为它已在第一个上下文中定义。是否有两个单独的上下文?一个包含
AspNetUserLogins
,另一个包含
Users
?,如果是这样,您需要在另一个包含
Users
的上下文中添加映射到
AspNetUserLogins
,然后您的工作应该正常。@Monah Yes是2 contexto 1 general,其他是to Identity。然后我需要重写OnModelCreating,在IdentityContext中添加aspnetTables和用户?对这很有意义。。。感谢我尝试这么做。添加
modelBuilder.Entity().HasKey(t=>t.Id)就足够了不需要其他映射,因为它已在第一个上下文中定义。
 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   

    }