neisbut / EntityFramework.MemoryJoin

Extension for EntityFramework for joins to in-memory data
MIT License
56 stars 24 forks source link

QueryModelClass is not found in the context. Please check configuration #12

Closed nguyenquynhan closed 4 years ago

nguyenquynhan commented 4 years ago

I have setup as your example but it doesn't work for me, I got the error as below. Do you have any ideas about that?

"System.InvalidOperationException: 'QueryModelClass is not found in the context. Please check configuration'"
   at EntityFramework.MemoryJoin.Internal.EFHelper.GetKeyProperty(DbContext context, Type t)
   at EntityFramework.MemoryJoin.Internal.MappingHelper.GetEntityMapping[T](DbContext context, Type queryClass, Dictionary`2 allowedPropertyMapping)
   at EntityFramework.MemoryJoin.MemoryJoiner.FromLocalList[T](DbContext context, IList`1 data, Type queryClass, ValuesInjectionMethod method)
   at EntityFramework.MemoryJoin.MemoryJoiner.FromLocalList[T](DbContext context, IList`1 data, Type queryClass)
   at EntityFramework.MemoryJoin.MemoryJoiner.FromLocalList[T](DbContext context, IList`1 data)
   at JoinMemoryListWithEF.Program.Main(String[] args) in C:\Users\Nhan.Nguyen\source\repos\JoinMemoryListWithEF\Program.cs:line 19

Below is my code:

namespace JoinMemoryListWithEF.Entity
{
    using EntityFramework.MemoryJoin;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Linq;

    public partial class ReviewManagementEntities : DbContext
    {
        public ReviewManagementEntities()
            : base("name=ReviewManagementEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<Review> Reviews { get; set; }
        protected DbSet<QueryModelClass> QueryData { get; set; }
    }
}

class Program
    {
        static void Main(string[] args)
        {
            var coachs = GetCoachs();     //List<Coach>      

            ReviewManagementEntities context = new ReviewManagementEntities();            
            var memmoryCoach = context.FromLocalList(coachs);// I got error at here

            Console.ReadLine();
        }

        private static List<Coach> GetCoachs()
        {
            return new List<Coach>()
            {
                new Coach() { CoachID = 149, Name="ABC" },
                new Coach() { CoachID = 160, Name="OK" }
            };
        }
    }

packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.2.0" targetFramework="net472" />
  <package id="EntityFrameworkCore.MemoryJoin" version="0.6.0" targetFramework="net472" />
  <package id="EntityFrameworkCore.MemoryJoin.Ef6" version="0.5.9" targetFramework="net472" />
</packages>
nguyenquynhan commented 4 years ago

You can see detail my project https://github.com/nguyenquynhan/JoinMemoryListWithEF

neisbut commented 4 years ago

Hi @nguyenquynhan, thanks for providing a sample project! That helps a lot! As I see MemJoin can't find QueryModelClass in DbContext metadata. Unfortunatlly since you use Database First approach simple adding DbSet<QueryModelClass> property to DbContext will not help. EF will not generate needed metadata. The only way I see is to add a NEW entity via designer. This entity type will be used for queries.

I did it like this (but you might want to add more properties to it and can name it differently):

image

Then used like this in code:

var memmoryCoach = context.FromLocalList(coachs, typeof(QueryModelClass2));

Please let me know if that helps.

neisbut commented 4 years ago

Closed due to inactivity