Register custom UserProfile in ASP.NET MVC4 results in duplicate tables

余生长醉 提交于 2019-12-13 00:14:23

问题


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");
    }
}

回答1:


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);
        }


来源:https://stackoverflow.com/questions/14836085/register-custom-userprofile-in-asp-net-mvc4-results-in-duplicate-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!