RAHUL
RAHUL

Reputation: 167

Nopcommmerce table generation

Can anyone please explain how does nopcommerce use DbSet for its Entities?

I wanted to know how does the NopObjectContext know about the tables in the database provided in the connection string.

My understanding was that in Code first, for a class that inherits from DbContext, there have to be getter and setter DbSet's for every Entity.

However I do not see this in the NopObjectContext. I am using version 2.6 where Code First is used.

Upvotes: 1

Views: 205

Answers (1)

AndyMcKenna
AndyMcKenna

Reputation: 2647

Your classes in Nop.Data.Mapping define the tables and properties.

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);
    }
}

Customer is defined under Nop.Core.Domain.Customers.Customer.cs. All of its properties will map to the table. The only parts you need to add here are what table to use, what properties to ignore, relationships, and properties for the fields (string length or precision).

Upvotes: 1

Related Questions