3

I want to create large user table (advance User Profile) and save user's data in my database context. So, I don't want to use 2 DbContexts in my project. When users register to site, they data (UserName, Password etc.) stores my own User table. My classes are like this:

public class ModelBase
    {
        public int Id { get; set; }
        public DateTime CreateDate { get; set; }
        public DateTime LastUpdateDate { get; set; }
    }

public class User : ModelBase
    {
        public string UserName { get; set; }
        public string Password{ get; set; }
        public string FullName { get; set; }
        public string Email { get; set; }
        public DateTime BirthDate { get; set; }
        public string Specialty { get; set; }
    }

public class News : ModelBase
    {
            public int UserId { get; set; }
            public string Title { get; set; }
            ...
    }

    ....

Context is so:

public class MyDBContext : DbContext
    {
        public MyDBContext()
        {
            Database.SetInitializer<MyDBContext>(new MyDBContextInitializer());
        }

        public DbSet<User> UserSet { get; set; }
        public DbSet<News> NewsSet { get; set; }
        public DbSet<Project> ProjectSet { get; set; }
        public DbSet<Section> SectionSet { get; set; }
        ....
    }

    class MyDBContextInitializer : DropCreateDatabaseIfModelChanges<MyDBContext>
        {
            protected override void Seed(MyDBContext context)
            {
                base.Seed(context);
            }
        }

I replaced DbContext name with mine and changed connection name in default SimpleMembershipInitializer class like this:

  ....
     Database.SetInitializer<MyDBContext>(null);

                try
                {
                    using (var context = new MyDBContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("MyDBContextConnection", "User", "Id", "UserName", autoCreateTables: true);
    ....

Finally, I changed RegisterModel and WebSecurity.CreateUserAndAccount() suitable my User class. But, it does not work. How can I use my own User table for register to site?

Jeyhun Rahimov
  • 3,769
  • 6
  • 47
  • 90

1 Answers1

1

You can have Asp.net Membership and your complex classes connected together. with this approach you will save so much time because asp.net membership is much more robust(you don't need to think about Role and User management) and sure you can make use of existing open source project like this and add it to your project with minimum effort of time. Then your class will have structure like :

public class CustomUserDetail : ModelBase
    {
        public string UserName { get; set; } // what you really need is this to be unique for each user in you data base
        // public string Password{ get; set; } handled by asp.net Membership
        public string FullName { get; set; }
        // public string Email { get; set; } handled by asp.net Membership
        public DateTime BirthDate { get; set; }
        public string Specialty { get; set; }
    }

Then you can can add extension method to IPrincipal like :

public static CustomUserDetail CustomUserDetail (this IPrincipal principal)
        {
            var repository = new  YourUserDetailRepository();
            return repository.GetCurrentUserDetail();
        }

and finnaly in your code easily use

<p> @User.CustomUserDetail.FullName  </p>
ghazyy
  • 649
  • 2
  • 7
  • 22