I'm trying to fetch records from database via SQL query and stored procedure, but in both case getting same error.
Cannot create a DbSet for 'CLASSMODEL' because this type is not included in the model for the context.
Here is my code.
I checked with existing nopCommerce plugin (Nop.Plugin.Shipping.FixedByWeightByTotal) and there getting same error message.
Here is my changes in Nop.Plugin.Shipping.FixedByWeightByTotal plugin.
Created new model under Model folder: ShippingRecordModel.cs
using Nop.Core;
namespace Nop.Plugin.Shipping.FixedByWeightByTotal.Model
{
/// <summary>
/// Represents a shipping by weight record
/// </summary>
public partial class ShippingRecordModel :BaseEntity
{
/// <summary>
/// Gets or sets the store identifier
/// </summary>
public int StoreId { get; set; }
/// <summary>
/// Gets or sets the warehouse identifier
/// </summary>
public int WarehouseId { get; set; }
/// <summary>
/// Gets or sets the country identifier
/// </summary>
public int CountryId { get; set; }
}
}
Modify ShippingByWeightByTotalObjectContext.cs file - update below methods
/// <summary>
/// Creates a LINQ query for the query type based on a raw SQL query
/// </summary>
/// <typeparam name="TQuery">Query type</typeparam>
/// <param name="sql">The raw SQL query</param>
/// <returns>An IQueryable representing the raw SQL query</returns>
public virtual IQueryable<TQuery> QueryFromSql<TQuery>(string sql) where TQuery : class
{
return this.Query<TQuery>().FromSql(sql);
}
/// <summary>
/// Creates a LINQ query for the entity based on a raw SQL query
/// </summary>
/// <typeparam name="TEntity">Entity type</typeparam>
/// <param name="sql">The raw SQL query</param>
/// <param name="parameters">The values to be assigned to parameters</param>
/// <returns>An IQueryable representing the raw SQL query</returns>
public virtual IQueryable<TEntity> EntityFromSql<TEntity>(string sql, params object[] parameters) where TEntity : BaseEntity
{
return this.Set<TEntity>().FromSql(CreateSqlWithParameters(sql, parameters), parameters);
}
/// <summary>
/// Modify the input SQL query by adding passed parameters
/// </summary>
/// <param name="sql">The raw SQL query</param>
/// <param name="parameters">The values to be assigned to parameters</param>
/// <returns>Modified raw SQL query</returns>
protected virtual string CreateSqlWithParameters(string sql, params object[] parameters)
{
//add parameters to sql
for (var i = 0; i <= (parameters?.Length ?? 0) - 1; i++)
{
if (!(parameters is DbParameter parameter))
continue;
sql = $"{sql}{(i > 0 ? "," : string.Empty)} @{parameter.ParameterName}";
//whether parameter is output
if (parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Output)
sql = $"{sql} output";
}
return sql;
}
Add this section in DependencyRegistrar.cs file
//override required repository with our custom context
builder.RegisterType<ShippingRecordModel>().As<ShippingRecordModel>()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_shipping_weight_total_zip"))
.InstancePerLifetimeScope();
Added new service in IShippingByWeightByTotalService.cs
IList<ShippingRecordModel> GetAllBySQL();
Implement this in ShippingByWeightByTotalService.cs
[i]In Field region
private readonly ShippingByWeightByTotalObjectContext _dbContext = EngineContext.Current.Resolve<ShippingByWeightByTotalObjectContext>();
In Mothod region
public virtual IList<ShippingRecordModel> GetAllBySQL()
{
var aa = _dbContext.QueryFromSql<ShippingRecordModel>("SELECT StoreId,WarehouseId,CountryId FROM ShippingMethod").ToList();
return aa;
}
In controller - FixedByWeightByTotalController.cs file - > Configure() method
add below line:
var allData = _shippingByWeightService.GetAllBySQL();
While code execute on this line, it's throw an exception.