简体   繁体   English

实体框架(代码优先)-&ASP.net成员资格提供程序

[英]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. 我目前正在尝试使用EF6代码构建新的应用程序。首先,我设法根据我的模型创建数据库没有问题。

Now what I want to do is use asp.net membership features. 现在我要做的是使用asp.net成员资格功能。 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. 互联网上有关于将simplemembershipprovider与EF一起使用的信息,但这非常令人困惑。

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? 那么,将成员资格与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 如果您使用的是MVC 5,则可以更改公共类AsoRockContext:DbContext

to be 成为

public class AsoRockContext : IdentityDbContext<ApplicationUser>

Then create your ApplicationUser 然后创建您的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. Asp.net MVC 4+附带了asp.net成员资格提供程序。 So you context name is TestConnection then 所以你的上下文名称是TestConnection然后

in the AccountModel in Model Directory, Change the defaultConnection to TestConnection like this, then it will create membership table in your DB. 在模型目录的AccountModel中,将defaultConnection更改为TestConnection如下所示,然后它将在您的数据库中创建成员资格表。

 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 要了解有关其实现方式的更多信息,请参见代码,AccountController文件,并在顶部[InitializeSimpleMembership]公共类AccountController:Controller中找到以下内容

Inspect the [InitializeSimpleMembership] attribute to learn how it is implemented. 检查[InitializeSimpleMembership]属性以了解其实现方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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