Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

BsonSerializationException when dealing with interfaces #12

Closed
yaseralnajjar opened this issue Sep 12, 2017 · 1 comment
Closed

BsonSerializationException when dealing with interfaces #12

yaseralnajjar opened this issue Sep 12, 2017 · 1 comment

Comments

@yaseralnajjar
Copy link

How to reproduce:

  1. Run the project.
  2. Add a car and fill the info.
  3. Stop the mongo server.*
  4. Run the mongo server and run the project again.
  5. Refresh couple of times, and this exception will show:
BsonSerializationException: Unknown discriminator value 'Employee'.
MongoDB.Bson.Serialization.BsonSerializer.LookupActualType(Type nominalType, BsonValue discriminator)

FormatException: An error occurred while deserializing the Owner property of class EFMongoDemo.Web.Models.Car: Unknown discriminator value 'Employee'.
MongoDB.Driver.Linq.MongoQueryProviderImpl.Execute(Expression expression)

This happens on the call:

var cars = await _context.Cars.ToListAsync();

And I noticed there isn't a collection called owner in the db after using the interface.

@crhairr
Copy link
Member

crhairr commented Sep 12, 2017

Duplicate of #9.

As I said in #13, interfaces aren't really support by EF Core, which is beyond the control of this project.

The only real issue is the navigation property. As I said in #9, navigations aren't fully supported yet, so you won't see that second Owner collection get created just because you added a Car. In the current implementation, Car.Owner will be treated as a subdocument, with the entire graph getting stored in a cars collection. For now, you should refactor your entities to use Car.OwnerId, and add the Owner instance directly to your DbContext, and then use a Join to look them up and project a view model that maps between the Car and Owner:

    _context.Cars
        .Join(_context.Employees,
            car => car.OwnerId,
            employee => employee.Id,
            (car, employee) => new CarAndOwnerModel
            {
                Car = car,
                Owner = employee
            });

I am working through the navigation implementation now, and I hope to have a solution for it soon.

@crhairr crhairr closed this as completed Sep 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants