实体框架(代码优先) - & ASP.net会员提供商

时间:2014-02-06 16:09:29

标签: c# asp.net entity-framework ef-code-first code-first

我目前正在尝试使用EF6 Code First构建一个新的应用程序,我设法根据我的模型创建数据库没问题。

现在我想要做的是使用asp.net会员功能。所以我必须在我的数据库中拥有数据库模式。

我该怎么做?

互联网上有关于使用simplemembershipprovider和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);
        }

    }

}

2 个答案:

答案 0 :(得分:0)

如果您正在使用MVC 5,那么您可以更改您的     公共类AsoRockContext:DbContext

public class AsoRockContext : IdentityDbContext<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; }
}

答案 1 :(得分:0)

Asp.net MVC 4+附带asp.net会员提供商。 因此,您的上下文名称为TestConnection然后

在模型目录的AccountModel中,将defaultConnection更改为TestConnection 像这样,它将在你的数据库中创建成员资格表。

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

要了解有关它是如何实现的更多信息,请参阅AccountController文件并在顶部找到以下内容      [InitializeSimpleMembership]      公共类AccountController:Controller

检查[InitializeSimpleMembership]属性以了解其实施方式。