Closed yaseralnajjar closed 5 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.
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.
I'm reopening this to help prompt me to get the documentation done.
Closing this. The documentation project has taken on a life of its own.
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:
But this doesn't even create a collection in the db when I add an owner, instead it just causes an exception:
I also tried specifiying which to dbset to add to by overriding the adding behavior in the OwnerRepository but same result:
For testing, the EFMongoDemo.Tests.DataTests.CarRepositoryShould.GetById_JoinCarAndOwner shows the same result.
*P.S: Using this will be fine:
*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.