简体   繁体   中英

Entity framework(Code first) - & ASP.net Membership Provider

I am currently trying to build a new application using EF6 Code First, I manage to create the database based on my models no problem.

Now what I want to do is use asp.net membership features. So I will have to have the database schema inside my database.

How can I do this?

There is information around the internet on using the simplemembershipprovider with EF, but it is very confusing.

Just so you have an idea of what I have done so far...(below)

So what is the best way to use membership with EF?

namespace AsoRock.Data.DTOs
{
    public class Customer
    {
        [Key]
        public int CustomerId { get; set; }
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string HomeNumber { get; set; }
        public string MobileNumber { get; set; }
        public string Gender { get; set; }
        public DateTime Dob { get; set; }
    }

    public class Order
    {
        [Key]
        public int OrderId { get; set; }
        public Address Address { get; set; }
        public Customer Customer { get; set; }
        public List<Product> Products { get; set; }
    }

    public class Product
    {
        [Key]
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public decimal ProductPrice { get; set; }
    }

    public class Address
    {
        [Key]
        public int AddressId { get; set; }
        public Customer Customer { get; set; }
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string City { get; set; }
        public string PostCode { get; set; }
        public string LastUsed { get; set; }
        public bool isDefault { get; set; }
    }


    public class AsoRockContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Address> Addresses { get; set; }
        public DbSet<Order> Orders { get; set; }
       // public DbSet<Product> Products { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Order>().HasMany(p => p.Products).WithMany().Map(m =>
            {
                m.MapLeftKey("OrderId").MapRightKey("ProductId").ToTable("OrderdProducts");
            });

            base.OnModelCreating(modelBuilder);
        }

    }

}

If you are using MVC 5 then you can change your public class AsoRockContext : DbContext

to be

public class AsoRockContext : IdentityDbContext<ApplicationUser>

Then create your ApplicationUser

public class ApplicationUser : IdentityUser
{   //You can add extra properties in if you want
    public string FirstName { get; set; }
    public string LastName { get; set; }
    //Or add a link to your customer table
    public Customer CustomerDetails { get; set; }
}

Asp.net MVC 4+ comes with asp.net membership provider. So you context name is TestConnection then

in the AccountModel in Model Directory, Change the defaultConnection to TestConnection like this, then it will create membership table in your DB.

 public UsersContext()
            : base("TestConnection")
        {
        }

to know more about how it is achieve see the code, see the AccountController file and find the following in top [InitializeSimpleMembership] public class AccountController : Controller

Inspect the [InitializeSimpleMembership] attribute to learn how it is implemented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM