Search code examples
asp.net-mvcentity-frameworksimplemembership

two user profile table created in sql server


I have my Accountmodels.cs class like this---

public class UsersContext : DbContext
{
    public UsersContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
}

public class RegisterExternalLoginModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    public string ExternalLoginData { get; set; }
}

and my User Profile class is like this---

  public class UserProfile
{
    public UserProfile()
    {
        this.Posts = new HashSet<Post>();
    }

    public int UserId { get; set; }
    public string UserName { get; set; }
    public string AvatarExt { get; set; }

    public virtual ICollection<Post> Posts { get; set; }

and finally my dbContext class is----

   public class WallEntities : DbContext
   {
    public WallEntities()
        : base("name=WallEntities")
    {
    }
    public DbSet<Post> Posts { get; set; }
    public DbSet<UserProfile> UserProfiles { get; set; }

I am using both WallEntities and DefaultConnection as name in connection string.I am using Simple Membership so i have a InitializeSimpleMembershipAttribute.cs File.Its look like this---

    private class SimpleMembershipInitializer
    {
        public SimpleMembershipInitializer()
        {
            Database.SetInitializer<UsersContext>(null);

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

My Problem is that It is creating two tables in Sql server.One is UserProfile and another is UserProfiles table.I want to create only 1 table that is UserProfile table with UserId,UserName and AvatarExt Property.After registration, it should store all information.


Solution

  • I want to answer my question is it may be useful for someone---

    Just The problem here was not to use the two DbContext.Instead of that, for the code First approach one dbContext is enough.

    So, DbContext should be like this----

      public class UsersContext : DbContext
      {
       public UsersContext()
        : base("DefaultConnection")
        {
        }
        public DbSet<Post> Posts { get; set; }
       it should not be here
       // public DbSet<UserProfile> UserProfiles { get; set; }
      }
    

    and romove that WallEntities DbContext. and reomove USerFrofile from UserContext as i have commented out and also change all the corresponding codes in the same way.