Comments (6)
The problem with using the code-first approach is that MongoDb BsonClassMaps are (infuriatingly) statically auto-generated. This means that only one instance can be created per app runtime, which leads to many different problems in terms of trying to map from an EF Core Model, which is a singleton that's created for each DbContext instance that references it. Because of how MongoDb automatically generates the class maps and immediately freezes them, the timing of trying to programmatically apply EF Core Model settings to a BsonClassMap can result in several different kinds of exceptions.
Because of this static generation of BsonClassMap, the recommended approach is to use aspect oriented model declarations and just stick with the annotations/attributes. You can use either [NotMapped] or [BsonIgnore] attributes on the member, and there are already conventions in place to keep the respective mappings in sync.
That said, I can add an IEntityTypeMemberIgnoredConvention implementation to the MongoDbConventionSet to attempt to apply the ignore status. But there's no guarantee that this will work, again because of the MongoDb C# driver's extremely poor design decision of relying heavily on static code over injectable and overridable services.
from entityframeworkcore.
Thanks for the feedback Chris, I agree. Since there is no better solution at this time, I will consider to create wrapper classes adding the recomended attributes for the purpose.
from entityframeworkcore.
Do you think that the static based usage of the mongodb driver could be masked by creating a internal wrapper class that manipulates app domains?
from entityframeworkcore.
.NET Core does not use app domains.
I might be able to work around the problem by adding an EntityType annotation that stores the BsonClassMap, and just create and manage them myself. That way, I should be able to prevent them from getting frozen, or at least guarantee that they're frozen only after the EF Core model is built. But that still won't get around any model mismatches if someone were to proactively create BsonClassMaps before allowing the EF Core model to be generated.
There's another request to allow mapping EntityTypes directly from existing BsonClassMaps, so I might be able to finagle that into some kind of bridge. Once the BsonClassMaps are created, I should be able to generate EntityTypes directly from them and not worry about mismatches in conventions. But this would require users to create BsonClassMaps rather than any of the code-first modeling from EF Core.
No matter how you slice it, the static architecture in the MongoDb C# driver is a pain to try to work around.
from entityframeworkcore.
Got it, Chris! Hopefully the mongodb team will improve the driver to conform better to the new .net core architecture
from entityframeworkcore.
I spent some time earlier in the year trying to work around this, and I couldn't figure out how to reliably force the BsonClassMaps to conform to the entity models without removing maps and re-creating them, which might cause problems at runtime if a map has already been created.
The safest way to work around this appears to be to use the aspect-oriented attributes (where available) for modeling, and allow the EFCore-MongoDB provider to generate the class maps in-sync with the EF models.
from entityframeworkcore.
Related Issues (20)
- Support for complex property HOT 1
- How can I use dotnet ef dbcontext scaffold HOT 1
- Multiple DbCntexts select query can't work concurrently. HOT 13
- Self refrence read not support HOT 3
- DbSet.Find throws InvalidOperationException HOT 2
- dbContext.DBSet.FirstOrDefault(e => e.FirstName == "xx") missing filter in MongoQueryable HOT 5
- bug: can not modify child HOT 7
- self refrence still can not work HOT 1
- Does this work with IdentityServer4 ? HOT 1
- MissingMethodException in Blueshift.EntityFrameworkCore.MongoDB.Metadata.Conventions.MongoDbRegisterKnownTypesConvention.Apply(InternalEntityTypeBuilder entityTypeBuilder) when using EF Core 2.2 HOT 2
- Wiki Getting Started Installing EFCore-MongoDb error HOT 1
- .NETCore 3 Preview 4 MongoDB.Driver needs upgrade HOT 2
- Still i got this Efcore internal classes problem HOT 1
- Get ObjectDisposedException when execute same query on differend DbContext instances
- Method not found exception
- Entity Framework 3.0 support? HOT 3
- What is the status of tis project? HOT 1
- Hangs on UseMongoDb HOT 2
- Missing Implementation for method HOT 1
- Missing Implementation for method 'get_Info' HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from entityframeworkcore.