The entity type TaxRateByCity is not part of the model for the current context.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 năm cách đây
In order to read data from the DB, I have done the following:

1) Added under Core.Domain.SDSharp folder one a TaxRateByCity.cs file
    using Nop.Core;
    namespace SDSharp.Core.Domain.Tax
    {
       public partial class TaxRateByCity : BaseEntity
       {
           public virtual string State { get; set; }
           public virtual string City { get; set; }
           public virtual float Rate { get; set; }
       }
    }

2) Added reference to SDSharp.Core.Domain.Tax on the SDSharpServices.cs file
    using SDSharp.Core.Domain.Tax;

3) Added a variable for the Service
    private readonly IRepository<TaxRateByCity> _taxratebycityRepository;

4) On Constructor I pass a reference of the Services:
         public SDSharpServicecs(IRepository<TaxRateByCity> taxratebycityRepository)
        {
            this._taxratebycityRepository = taxratebycityRepository;
        }
  

=======================
When I run the Shopping Cart, the following code is executed:

        public virtual decimal GetTaxRateByCity(string state, string city)
        {
            var query = from c in _taxratebycityRepository.Table
                        where (c.City == city) && (c.State == state)
                        select c.Rate;

            decimal rate = Convert.ToDecimal(query.FirstOrDefault());
            return rate;
        }


ERROR:
The entity type TaxRateByCity is not part of the model for the current context.

Any idea what am I missing ??
How does the Domain Model get attached to the actual Table?
Notice i do NOT have a PK on that Domain, is this a problem?
12 năm cách đây
You added a new entity (TaxRateByCity) and want to use it. But default context NopObjectContext doesn't contains this model. You need to create a new implementation of IDbContext. Don't forget to register it in DependencyRegistrar (IRepository<TaxRateByCity> injected into your service should use new IDbContext). I presume that haven't done it. In order to get an idea, have a look Nop.Plugin.Tax.CountryStateZip plugin (it has a custom context - TaxRateObjectContext)
12 năm cách đây
I thought you create a plugin. But after looking at namespaces, got it. All you need to do is to add a mapping class to 'Nop.Data' assembly. For example, look at \Libraries\Nop.Data\Mapping\Discounts\DiscountMap.cs
12 năm cách đây
That was it ...i just added the following class and all worked perfectly.


namespace SDSharp.Data.Mapping
{
    public class TaxRateByCityMap : EntityTypeConfiguration<TaxRateByCity>
    {
        public TaxRateByCityMap()
        {
            this.ToTable("TaxRateByCity");
            this.HasKey(tax => tax.TaxRateID);
            this.Property(tax => tax.State).IsRequired().HasMaxLength(50);
            this.Property(tax => tax.County).IsOptional();
            this.Property(tax => tax.City).IsRequired().HasMaxLength(50);
            this.Property(tax => tax.Rate).IsRequired();
        }
    }
}


===========================
THANKS A LOT !!!
11 năm cách đây
a.m. wrote:
I thought you create a plugin. But after looking at namespaces, got it. All you need to do is to add a mapping class to 'Nop.Data' assembly. For example, look at \Libraries\Nop.Data\Mapping\Discounts\DiscountMap.cs


a.m,

I am getting same error but my question is that, If I will not give any Data Mapping class then it will give an error as you say, but In my situation if I give two Data Mapping classes of same name and same entity then will it give an error? Because my situation is this and I can't change/remove unnecessary mapping class from there now.

Please give some advice. Thanks.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.