【发布时间】:2019-10-04 01:28:00
【问题描述】:
如何使用 Entity Framework 6.3 映射代码中的表值函数?我正在尝试使用现有数据库的代码中的数据库上下文,因为 ASP.NET Core 3 目前不支持 EDMX。我尝试如下设置我的 DbContext clasee。我可以成功查询Grade表。但是当我尝试查询我的函数“fn_GetCatgeories”时,我收到以下错误:No EdmType found for type 'WebApplication6.Data.ApplicationContext+fn_GetCategories'。
public class ApplicationContext : DbContext
{
public ApplicationContext(string cstr)
: base(cstr)
{
Database.SetInitializer<ApplicationContext>(null);
}
[Table("Grade")]
public class Grade
{
[Key]
public string Grade_ID { get; set; }
public string SchoolType { get; set; }
public int Sortorder { get; set; }
}
public partial class fn_GetCategories
{
public int Category_ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Nullable<bool> Active { get; set; }
public Nullable<System.DateTime> Month { get; set; }
public Nullable<int> Order { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(new FunctionsConvention<ApplicationContext>("dbo"));
base.OnModelCreating(modelBuilder);
}
[DbFunction("ApplicationContext", "fn_GetCategories")]
public IQueryable<fn_GetCategories> GetCategories(Nullable<System.DateTime> month)
{
var monthParameter = month.HasValue ?
new ObjectParameter("Month", month) :
new ObjectParameter("Month", typeof(System.DateTime));
return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<fn_GetCategories>(string.Format("[0].{1}", GetType().Name, "[fn_GetCategories](@Month)"), monthParameter);
}
// DbSets here
public DbSet<Grade> Grades { get; set; }
}
【问题讨论】: