Отображение функции табличного значения с помощью Entity Framework 6.3 - PullRequest
0 голосов
/ 04 октября 2019

Как мне сопоставить функции табличных значений в коде с Entity Framework 6.3? Я пытаюсь использовать контекст БД в коде с существующей базой данных, потому что EDMX в настоящее время не поддерживается в ASP.NET Core 3. Я попытался настроить мое приложение DbContext, как показано ниже. Я могу успешно запросить таблицу оценок. Но когда я пытаюсь выполнить запрос к своей функции "fn_GetCatgeories", я получаю следующую ошибку: EdmType не найден для типа '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; }


}

1 Ответ

0 голосов
/ 04 октября 2019

Это работает:

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();

        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...