BlueshiftSoftware / EntityFrameworkCore

NoSQL providers for EntityFramework Core
Other
281 stars 57 forks source link

InvalidOperationException when adding derived to a base-type dbset #14

Closed yaseralnajjar closed 5 years ago

yaseralnajjar commented 7 years ago

Hi,

I added many many stuff into the demo repo: https://github.com/0xRumple/EFMongoDemo

I started using a base class for the owners (Employee and Manager) which is called Owner instead of the IOwner interface when talking to the database cuz as you the EF core doesn't support that even in In-Memory unit tests 😞 .

In the dbsets, I used:

public DbSet<Owner> Owners { get; set; }

But this doesn't even create a collection in the db when I add an owner, instead it just causes an exception:

InvalidOperationException: The entity type 'Employee' was not found. Ensure that the entity type has been added to the model.

I also tried specifiying which to dbset to add to by overriding the adding behavior in the OwnerRepository but same result:

public override async Task<Owner> Add(Owner entity)
    {
        Context.Owners.Add(entity);
        await CommitChanges();

        return entity;
    }

For testing, the EFMongoDemo.Tests.DataTests.CarRepositoryShould.GetById_JoinCarAndOwner shows the same result.

*P.S: Using this will be fine:

public DbSet<Employee> Owners { get; set; }

*P.P.S: In mssql provider if I use the base type, it will annoyingly map all the properties in both derived types, in this case both AllowedNumberToOwn and AffliatedEmployees will be mapped into one table called "Owners" to satisfy the EF.

crhairr commented 7 years ago

Are you using the [BsonKnownTypes] attribute to declared derived types or adding them to the model on OnModelCreating?

The model is built when the DbContext is first loaded up - this is by design of EF Core itself, not a peculiarity of EFCore-MongoDB. The Model is built off of the declared DbSet properties on the DbContext, with an opportunity to expand on that through the OnModelCreating method. The EF Core model only knows about what is declared or manually registered on the model. That is, it cannot at runtime detect that Employee is derived from Owner, and thus should go through that set.

The underlying MongoDB C# provides a declarative way to register known derived types using BsonKnownTypesAttribute. There are tests for polymorphic entities.

Please review the sample domain and related tests before submitting any further issues.

crhairr commented 7 years ago

FWIW, I'm also working on expanding the documentation. The Getting Started wiki is admittedly a bit weak right now. I do plan on expanding on it quite a bit, with deeper linking to the documentation for both EF Core and the MongoDB C# driver to help capture and explain the underlying mechanisms.

crhairr commented 7 years ago

I'm reopening this to help prompt me to get the documentation done.

crhairr commented 5 years ago

Closing this. The documentation project has taken on a life of its own.