2012-11-25 71 views
1

任何人都可以請解釋nopcommerce如何使用DbSet爲其實體?Nopcommmerce表格生成

我想知道NopObjectContext如何知道連接字符串中提供的數據庫中的表。

我的理解是,在Code首先,對於繼承自DbContext的類,每個實體必須有getter和setter DbSet。

但是我沒有在NopObjectContext中看到這個。我在使用Code First的2.6版本。

回答

1

Nop.Data.Mapping中的類定義表和屬性。

public partial class CustomerMap : EntityTypeConfiguration<Customer> 
{ 
    public CustomerMap() 
    { 
     this.ToTable("Customer"); 
     this.HasKey(c => c.Id); 
     this.Property(u => u.Username).HasMaxLength(1000); 
     this.Property(u => u.Email).HasMaxLength(1000); 
     this.Property(u => u.Password); 
     this.Property(c => c.AdminComment).IsMaxLength(); 
     this.Property(c => c.CheckoutAttributes).IsMaxLength(); 
     this.Property(c => c.GiftCardCouponCodes).IsMaxLength(); 

     this.Ignore(u => u.PasswordFormat); 
     this.Ignore(c => c.TaxDisplayType); 
     this.Ignore(c => c.VatNumberStatus); 

     this.HasOptional(c => c.Language) 
      .WithMany() 
      .HasForeignKey(c => c.LanguageId).WillCascadeOnDelete(false); 

     this.HasOptional(c => c.Currency) 
      .WithMany() 
      .HasForeignKey(c => c.CurrencyId).WillCascadeOnDelete(false); 

     this.HasMany(c => c.CustomerRoles) 
      .WithMany() 
      .Map(m => m.ToTable("Customer_CustomerRole_Mapping")); 

     this.HasMany(c => c.DismissedAttributeNotices) 
      .WithMany() 
      .Map(m => m.ToTable("ProductAttribute_DismissedAttributeNotices")); 

     this.HasMany<Address>(c => c.Addresses) 
      .WithMany() 
      .Map(m => m.ToTable("CustomerAddresses")); 
     this.HasOptional<Address>(c => c.BillingAddress); 
     this.HasOptional<Address>(c => c.ShippingAddress); 
     this.HasOptional<Customer>(c => c.SelectedSalesRep); 
    } 
} 

客戶在Nop.Core.Domain.Customers.Customer.cs下定義。其所有屬性都將映射到該表。您需要添加的唯一部分是要使用的表格,要忽略哪些屬性,關係和字段屬性(字符串長度或精度)。