2014-02-06 137 views
1

我目前正在嘗試使用EF6 Code構建一個新的應用程序,我設法基於我的模型創建數據庫沒有問題。實體框架(代碼優先) - &ASP.net會員提供

現在我想要做的是使用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); 
     } 

    } 

} 
+0

哪個版本的asp.net mvc? – Miller

回答

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; } 
} 
+0

你可以給我一些更多的信息。我目前在我的解決方案中只有一個項目(AsoRock.Data)
這是我放置所有EF代碼的地方。 (Code First)所以如果我創建一個MVC5項目(AsoRock.Web),你是否說我會在AsoRock中調用AsoRockContext.Data –

+0

你可以給我更多的信息。我目前在我的解決方案中只有一個項目(AsoRock.Data)這是我將所有EF代碼放在哪裏。 (Code First)所以如果我創建一個MVC5項目(AsoRock.Web),你是否說我會在AsoRock.Data中調用AsoRockContext,當我更新模型時,這會如何影響數據庫的自動創建? @ASG –

+0

你可以把你的Context(和DbSets)放到你的MVC項目中,然後添加對你的AsoRock.Data項目的引用 – ASG

0

天冬氨酸。淨MVC 4 +自帶asp.net會員供應商。 所以你上下文名稱爲TestConnection然後

在型號目錄的AccountModel,更改defaultConnectionTestConnection 這樣,那麼它會在你的數據庫創建成員表。

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

更多地瞭解它是如何實現查看代碼,看到的AccountController文件,並找到在頂部 [InitializeSimpleMembership] 公共類的AccountController如下:控制器

檢查[InitializeSimpleMembership]屬性,以瞭解如何它被執行。