注册自定义用户配置在ASP.NET MVC4结果表一式两份 [英] Register custom UserProfile in ASP.NET MVC4 results in duplicate tables

查看:79
本文介绍了注册自定义用户配置在ASP.NET MVC4结果表一式两份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想存储在一个ASP.NET MVC4互联网应用的自定义用户配置类。用户配置类名为Usuario,看起来像这样:

  [表(USUARIOS)]
公共类Usuario
{
    [键]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)
    公众诠释用户ID {搞定;组; }
    公共字符串用户名{获得;组; }
    [电子邮件地址]
    公共字符串邮报{搞定;组; }
    公共字符串微博{搞定;组; }
    公共字符串PERFIL {搞定;组; }
    公共字符串LinkedIn {搞定;组; }
    公开名单< Evento> Ponencias {搞定;组; }
    公开名单< Evento> Asistencias {搞定;组; }
    公开名单< Evento> EventosCreados {搞定;组; }
}

我也做了叫WeventioDb一个Context类,看起来像这样:

 公共类WeventioDb:的DbContext
{
    公共DbSet< Usuario> USUARIOS {搞定;组; }
    公共DbSet< Evento> EVENTOS {搞定;组; }
    公共DbSet< Comentario>备注{搞定;组; }
    公共DbSet< Ubicacion> Ubicaciones {搞定;组; }
}

<强>问题是,当一个用户寄存器,它不被存储到USUARIOS表时,它被存储到Usuario(注意在末端缺失S)和只用一个UserName属性和用户ID。我想,当一个用户寄存器,它被存储到USUARIOS表

我的注册动作看起来是这样的:

 公众的ActionResult寄存器(RegisterModel模型)
    {
        如果(ModelState.IsValid)
        {
            尝试
            {
                WebSecurity.CreateUserAndAccount(model.UserName,model.Password);
                WebSecurity.Login(model.UserName,model.Password);
                返回RedirectToAction(指数,家);
            }
            赶上(MembershipCreateUserException E)
            {
                ModelState.AddModelError(,错误codeToString(e.Status code));
            }
        }        返回查看(模型);
    }

这是如何创建一个用户并将其存储到USUARIOS表,并与会员使用吗?任何想法

UPADTE:这是我最初的迁移(创建表)看起来,它正在创建右表USUARIOS,但是当我去.mdf文件中,USUARIOS表是空的,所有的注册用户,就像以前描述的Usuario

 公共部分类初始:DbMigration
{
    公共覆盖无效向上()
    {
        CREATETABLE(
            dbo.Usuarios
            C =&GT;新
                {
                    用户ID = c.Int(可空:假的,身份:真)
                    用户名= c.String(),
                    邮报= c.String(),
                    微博= c.String()
                    PERFIL = c.String(),
                    LinkedIn = c.String()
                    Evento_Id = c.Int(),
                    Evento_Id1 = c.Int(),
                })
            .PrimaryKey(T =&GT; t.UserId)
            .ForeignKey(dbo.Eventos,T =&GT; t.Evento_Id)
            .ForeignKey(dbo.Eventos,T =&GT; t.Evento_Id1)
            的.index(T =&GT; t.Evento_Id)
            的.index(T =&GT; t.Evento_Id1);        CREATETABLE(
            dbo.Eventos
            C =&GT;新
                {
                    ID = c.Int(可空:假的,身份:真)
                    出生日期= c.DateTime(可空:假),
                    preCIO = c.Double(可空:假),
                    Descripcion = c.String(),
                    架构= c.String(),
                    部门= c.String()
                    横幅= c.String()
                    imagen画质= c.String()
                    微博= c.String()
                    包括hashtag = c.String()
                    Programa = c.String(),
                    Organizador_UserId = c.Int(),
                    Rating_ID = c.Int(),
                    Sitio_Id = c.Int(),
                    Usuario_UserId = c.Int(),
                    Usuario_UserId1 = c.Int(),
                    Usuario_UserId2 = c.Int(),
                })
            .PrimaryKey(T =&GT; t.Id)
            .ForeignKey(dbo.Usuarios,T =&GT; t.Organizador_UserId)
            .ForeignKey(dbo.Ratings,T =&GT; t.Rating_ID)
            .ForeignKey(dbo.Ubicacions,T =&GT; t.Sitio_Id)
            .ForeignKey(dbo.Usuarios,T =&GT; t.Usuario_UserId)
            .ForeignKey(dbo.Usuarios,T =&GT; t.Usuario_UserId1)
            .ForeignKey(dbo.Usuarios,T =&GT; t.Usuario_UserId2)
            的.index(T =&GT; t.Organizador_UserId)
            的.index(T =&GT; t.Rating_ID)
            的.index(T =&GT; t.Sitio_Id)
            的.index(T =&GT; t.Usuario_UserId)
            的.index(T =&GT; t.Usuario_UserId1)
            的.index(T =&GT; t.Usuario_UserId2);        CREATETABLE(
            dbo.Ratings
            C =&GT;新
                {
                    ID = c.Int(可空:假的,身份:真)
                    Texto = c.String(),
                    Puntaje = c.Int(可空:假),
                    出生日期= c.DateTime(可空:假),
                    Autor_UserId = c.Int(),
                })
            .PrimaryKey(T =&GT; t.ID)
            .ForeignKey(dbo.Usuarios,T =&GT; t.Autor_UserId)
            的.index(T =&GT; t.Autor_UserId);        CREATETABLE(
            dbo.Ubicacions
            C =&GT;新
                {
                    ID = c.Int(可空:假的,身份:真)
                    Latitud = c.Double(可空:假),
                    Longitud = c.Double(可空:假),
                    Direccion = c.String(),
                    农布雷= c.String(),
                    微博= c.String()
                    CodigoPostal = c.Int(可空:假),
                    城= c.String()
                    埃斯塔= c.String()
                })
            .PrimaryKey(T =&GT; t.Id);        CREATETABLE(
            dbo.Comentarios
            C =&GT;新
                {
                    ID = c.Int(可空:假的,身份:真)
                    Texto = c.String(),
                    出生日期= c.DateTime(可空:假),
                    Autor_UserId = c.Int(),
                    Evento_Id = c.Int(),
                })
            .PrimaryKey(T =&GT; t.Id)
            .ForeignKey(dbo.Usuarios,T =&GT; t.Autor_UserId)
            .ForeignKey(dbo.Eventos,T =&GT; t.Evento_Id)
            的.index(T =&GT; t.Autor_UserId)
            的.index(T =&GT; t.Evento_Id);    }    公共覆盖无效向下()
    {
        DropIndex(dbo.Comentarios,新的[] {Evento_Id});
        DropIndex(dbo.Comentarios,新的[] {Autor_UserId});
        DropIndex(dbo.Ratings,新的[] {Autor_UserId});
        DropIndex(dbo.Eventos,新的[] {Usuario_UserId2});
        DropIndex(dbo.Eventos,新的[] {Usuario_UserId1});
        DropIndex(dbo.Eventos,新的[] {Usuario_UserId});
        DropIndex(dbo.Eventos,新的[] {Sitio_Id});
        DropIndex(dbo.Eventos,新的[] {Rating_ID});
        DropIndex(dbo.Eventos,新的[] {Organizador_UserId});
        DropIndex(dbo.Usuarios,新的[] {Evento_Id1});
        DropIndex(dbo.Usuarios,新的[] {Evento_Id});
        DropForeignKey(dbo.Comentarios,Evento_Id,dbo.Eventos);
        DropForeignKey(dbo.Comentarios,Autor_UserId,dbo.Usuarios);
        DropForeignKey(dbo.Ratings,Autor_UserId,dbo.Usuarios);
        DropForeignKey(dbo.Eventos,Usuario_UserId2,dbo.Usuarios);
        DropForeignKey(dbo.Eventos,Usuario_UserId1,dbo.Usuarios);
        DropForeignKey(dbo.Eventos,Usuario_UserId,dbo.Usuarios);
        DropForeignKey(dbo.Eventos,Sitio_Id,dbo.Ubicacions);
        DropForeignKey(dbo.Eventos,Rating_ID,dbo.Ratings);
        DropForeignKey(dbo.Eventos,Organizador_UserId,dbo.Usuarios);
        DropForeignKey(dbo.Usuarios,Evento_Id1,dbo.Eventos);
        DropForeignKey(dbo.Usuarios,Evento_Id,dbo.Eventos);
        DROPTABLE(dbo.Comentarios);
        DROPTABLE(dbo.Ubicacions);
        DROPTABLE(dbo.Ratings);
        DROPTABLE(dbo.Eventos);
        DROPTABLE(dbo.Usuarios);
    }
}


解决方案

的问题是与InitializeSimpleMembership类,因此删除了[InitializeSimpleMembership]注释和类,并在Global.asax.cs中添加:

  VAR迁移=新DbMigrator(新配置());
        migrator.Update();        如果(!WebSecurity.Initialized)
        {
            WebSecurity.InitializeDatabaseConnection(DefaultConnection,用户配置,用户ID,用户名,autoCreateTables:真正的);
        }

I'm trying to store a custom UserProfile class in an ASP.NET MVC4 Internet application. UserProfile class is named "Usuario" and looks like this:

[Table("Usuarios")]
public class Usuario
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    [EmailAddress]
    public string Correo { get; set; }
    public string Twitter { get; set; }
    public string Perfil { get; set; }
    public string LinkedIn { get; set; }
    public List<Evento> Ponencias { get; set; }
    public List<Evento> Asistencias { get; set; }
    public List<Evento> EventosCreados { get; set; }
}

I've also made a Context class called WeventioDb and looks like this:

 public class WeventioDb : DbContext
{
    public DbSet<Usuario> Usuarios { get; set; }
    public DbSet<Evento> Eventos { get; set; }
    public DbSet<Comentario> Comentarios { get; set; }
    public DbSet<Ubicacion> Ubicaciones { get; set; }
}

The problem is that when an user registers, it is not stored into the "Usuarios" table, it is stored into the "Usuario" (note the missing S at the end) and only with an UserName property, and UserId. I want that when an user registers, it is stored into the "Usuarios" table.

My Register action looks like this:

 public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                WebSecurity.Login(model.UserName, model.Password);
                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        return View(model);
    }

Any ideas on how to create an user and store it into the "Usuarios" table and use it with Membership?

Upadte: this is what my initial migration (table creation) looks like, it is creating the right table "Usuarios", but when I go to the .mdf file, the "Usuarios" table is empty and all registered users, in "Usuario" just as described before:

public partial class Initial : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Usuarios",
            c => new
                {
                    UserId = c.Int(nullable: false, identity: true),
                    UserName = c.String(),
                    Correo = c.String(),
                    Twitter = c.String(),
                    Perfil = c.String(),
                    LinkedIn = c.String(),
                    Evento_Id = c.Int(),
                    Evento_Id1 = c.Int(),
                })
            .PrimaryKey(t => t.UserId)
            .ForeignKey("dbo.Eventos", t => t.Evento_Id)
            .ForeignKey("dbo.Eventos", t => t.Evento_Id1)
            .Index(t => t.Evento_Id)
            .Index(t => t.Evento_Id1);

        CreateTable(
            "dbo.Eventos",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Fecha = c.DateTime(nullable: false),
                    Precio = c.Double(nullable: false),
                    Descripcion = c.String(),
                    Schema = c.String(),
                    Sector = c.String(),
                    Banner = c.String(),
                    Imagen = c.String(),
                    Twitter = c.String(),
                    Hashtag = c.String(),
                    Programa = c.String(),
                    Organizador_UserId = c.Int(),
                    Rating_ID = c.Int(),
                    Sitio_Id = c.Int(),
                    Usuario_UserId = c.Int(),
                    Usuario_UserId1 = c.Int(),
                    Usuario_UserId2 = c.Int(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Usuarios", t => t.Organizador_UserId)
            .ForeignKey("dbo.Ratings", t => t.Rating_ID)
            .ForeignKey("dbo.Ubicacions", t => t.Sitio_Id)
            .ForeignKey("dbo.Usuarios", t => t.Usuario_UserId)
            .ForeignKey("dbo.Usuarios", t => t.Usuario_UserId1)
            .ForeignKey("dbo.Usuarios", t => t.Usuario_UserId2)
            .Index(t => t.Organizador_UserId)
            .Index(t => t.Rating_ID)
            .Index(t => t.Sitio_Id)
            .Index(t => t.Usuario_UserId)
            .Index(t => t.Usuario_UserId1)
            .Index(t => t.Usuario_UserId2);

        CreateTable(
            "dbo.Ratings",
            c => new
                {
                    ID = c.Int(nullable: false, identity: true),
                    Texto = c.String(),
                    Puntaje = c.Int(nullable: false),
                    Fecha = c.DateTime(nullable: false),
                    Autor_UserId = c.Int(),
                })
            .PrimaryKey(t => t.ID)
            .ForeignKey("dbo.Usuarios", t => t.Autor_UserId)
            .Index(t => t.Autor_UserId);

        CreateTable(
            "dbo.Ubicacions",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Latitud = c.Double(nullable: false),
                    Longitud = c.Double(nullable: false),
                    Direccion = c.String(),
                    Nombre = c.String(),
                    Twitter = c.String(),
                    CodigoPostal = c.Int(nullable: false),
                    Ciudad = c.String(),
                    Estado = c.String(),
                })
            .PrimaryKey(t => t.Id);

        CreateTable(
            "dbo.Comentarios",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Texto = c.String(),
                    Fecha = c.DateTime(nullable: false),
                    Autor_UserId = c.Int(),
                    Evento_Id = c.Int(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Usuarios", t => t.Autor_UserId)
            .ForeignKey("dbo.Eventos", t => t.Evento_Id)
            .Index(t => t.Autor_UserId)
            .Index(t => t.Evento_Id);

    }

    public override void Down()
    {
        DropIndex("dbo.Comentarios", new[] { "Evento_Id" });
        DropIndex("dbo.Comentarios", new[] { "Autor_UserId" });
        DropIndex("dbo.Ratings", new[] { "Autor_UserId" });
        DropIndex("dbo.Eventos", new[] { "Usuario_UserId2" });
        DropIndex("dbo.Eventos", new[] { "Usuario_UserId1" });
        DropIndex("dbo.Eventos", new[] { "Usuario_UserId" });
        DropIndex("dbo.Eventos", new[] { "Sitio_Id" });
        DropIndex("dbo.Eventos", new[] { "Rating_ID" });
        DropIndex("dbo.Eventos", new[] { "Organizador_UserId" });
        DropIndex("dbo.Usuarios", new[] { "Evento_Id1" });
        DropIndex("dbo.Usuarios", new[] { "Evento_Id" });
        DropForeignKey("dbo.Comentarios", "Evento_Id", "dbo.Eventos");
        DropForeignKey("dbo.Comentarios", "Autor_UserId", "dbo.Usuarios");
        DropForeignKey("dbo.Ratings", "Autor_UserId", "dbo.Usuarios");
        DropForeignKey("dbo.Eventos", "Usuario_UserId2", "dbo.Usuarios");
        DropForeignKey("dbo.Eventos", "Usuario_UserId1", "dbo.Usuarios");
        DropForeignKey("dbo.Eventos", "Usuario_UserId", "dbo.Usuarios");
        DropForeignKey("dbo.Eventos", "Sitio_Id", "dbo.Ubicacions");
        DropForeignKey("dbo.Eventos", "Rating_ID", "dbo.Ratings");
        DropForeignKey("dbo.Eventos", "Organizador_UserId", "dbo.Usuarios");
        DropForeignKey("dbo.Usuarios", "Evento_Id1", "dbo.Eventos");
        DropForeignKey("dbo.Usuarios", "Evento_Id", "dbo.Eventos");
        DropTable("dbo.Comentarios");
        DropTable("dbo.Ubicacions");
        DropTable("dbo.Ratings");
        DropTable("dbo.Eventos");
        DropTable("dbo.Usuarios");
    }
}

解决方案

The problem was with the InitializeSimpleMembership class, so removed the [InitializeSimpleMembership] annotation and class and added in the Global.asax.cs:

 var migrator = new DbMigrator(new Configuration());
        migrator.Update();

        if (!WebSecurity.Initialized)
        {
            WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
        }

这篇关于注册自定义用户配置在ASP.NET MVC4结果表一式两份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆