Invalid Object Name "<tablename>" after creating table

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 năm cách đây
Hello everyone,

I absolutely make sure that I have created some tables before in Nopcommerce before. But today, when I try to add new a table I got an error "Invalid Object Name "dbo.District" I am sure that I see the table was created in the database, but the repository just cant find it?

This is the district table I want to add to the database:

public class District : BaseEntity, ILocalizedEntity
    {
        public string Title { get; set; }
        public ICollection<Product2District> Products { get; set; } = new List<Product2District>();
    }


In the Product Entity I add a line to relationship with the 3rd table:

public ICollection<Product2District> Districts { get; set; } = new List<Product2District>();


And the 3rd table to have many to many data between them:

public partial class Product2District : BaseEntity
    {
        public virtual Product Product { get; set; }
        public virtual District District { get; set; }
    }


DistrictMap.cs:
public DistrictMap()
        {
            this.ToTable("District");
            this.HasKey(p => p.Id);
            this.HasMany(p => p.Products)
                .WithOptional(p => p.District);
        }


In the ProductMap.cs, I added a line:
this.HasMany(p => p.Districts)
                .WithOptional(pt => pt.Product);


I dont understand what I miss? *the table was created in database, but I cant use them.
7 năm cách đây
Please, help me. This error makes me crazy
7 năm cách đây
Please show us your OnModelCreating code, you may missing to add there!
In that case you won't get a compile error. But you will get a runtime error when you use the entity.

        
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Configurations.Add(new DistrictMap());
   modelBuilder.Configurations.Add(new ProductMap());

   base.OnModelCreating(modelBuilder);
}
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.