【问题标题】:Table Value Function mapping with Entity Framework 6.3实体框架 6.3 的表值函数映射
【发布时间】: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; }


}

【问题讨论】:

    标签: entity-framework-6


    【解决方案1】:

    这行得通:

    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_Result
            {
                [Key]    
                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; }
            }
    
    
            // DbSets here
            public DbSet<Grade> Grades { get; set; }  
            public DbSet<fn_GetCategories_Result> fn_GetCategoriesSet { get; set; }
    
    
            public List<fn_GetCategories_Result> fn_GetCategories(DateTime month)
            {
                var m = new SqlParameter("Month", DateTime.Now);
                return this.fn_GetCategoriesSet.SqlQuery("select * from  dbo.fn_GetCategories(@month)", m).ToList();
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      • 2010-10-09
      • 2011-11-16
      • 2016-02-21
      • 1970-01-01
      相关资源
      最近更新 更多