I can see in the manager extensions, that exists EF 4.x dbcontext and EF 4.x POCO. Both of them generate entities for persistence ignorant.
What are the difference between them?
What are the difference between 4.x and 5.x? which is the new features of the 5.x version?
Is it possible to use only one exchange entities for all the ways to access to the database? I would like to use an interface to use different ways to access to the database. EF 4, EF 5 and others than not necessary it would be EF. I would like to program once and use it in many scenarios.
DbContext is your gateway to all the entities in your model. For instance, if your context was called con, you might call con.Orders() to get to your Order objects. POCOs (plain old CLR objects) generated with Entity Framework do not extend classes from the Entity Framework. POCOs with Entity Framework are more advanced, so I recommend you not use them when getting started. One of the new features with EF 5 is replacement of DbContext with ObjectContext. OK, not replacement, because you can still use the DbContext. But Microsoft wants ObjectContext to be the new simpler way to access all your entities instead of DbContext. ObjectContext is much easier to work with in most cases.
Related
I have entities on which I don't want to have a default constructor because the constructor has logic and enforces invariants.
I would like to be able to configure Entity Framework Core to use a specific constructor in a certain way.
The workaround I use for now is have purely data-oriented classes (sort of "Database DTOs") for mapping entities to database, but this is not very satisfying I would like to use my domain entity classes with EF Core.
I know that EF Core can use a constructor having parameters matching the properties. But it is too limited for some use cases. For instance, I have an entity which implements the State design pattern, meaning when retrieved from the database, a state class must be recreated from a string using a factory. EF Core can't figure that on its own and I've not yet a way to configure how entities are constructed when reconstituted from the database.
Do you know if it is possible in EF Core ?
Thanks you in advance,
I'm building REST APIs with ASP.NET Core and Entity Framework Core with a database-first approach with clean architecture.
My database is already created and maintained by a DBA team - that's why I need to use Entity Framework Core with a database-first approach, and we need to do reverse engineering using the Scaffold-DbContext command in the infrastructure layer.
With clean architecture, entities should be placed in Core layer separate from the infrastructure layer so how we will do that and how to resolve your DbContext as an interface.
I see two options:
You accept the dependency to the EF, put the generated entities into your core project and so accept the violation to clean architecture.
You create your own entities in Core project without dependencies to EF and use repository pattern to "map" between your entities and those from EF.
Alternative 1 is simple and cheap. Alternative 2 gives you independence from EF and some abstraction to your dba team. Choose based on what is more important for your project.
DbContext have properties reflecting the different sets of entities in the database.
I'm using a plugin architecture using MEF and have read similar SO question answering part of my question (MEF Plugins and EF CodeFirst - How?)
But in my core app, I have multiple DbContext (derivatives) that group handfuls of entities together. e.g. SecurityDbContext ConfigurationDbContext.
These contexts only have a few DbSet properties in them.
The article i referenced provides a method by which you have one central DbContext and configure the models in the plugin libraries. This would mean there is one DbContext for the whole application and plugins.
I know EF6 support multiple DbContexts per DB - so does that mean I can just create as many DbContexts as needed - including in the plugins?
I'm assuming that a DbContext can only join (in queries) to entities defined in it irrespective of whether the DbContext is for the same DB.
Any advice appreciated
I thought I'd share my conclusion after reading all the links other users have provided.
It was clear that the DbContext classes I had created didn't include enough entities as I found myself using helper functions I'd written to detach entity objects from other contexts so that they could be attached to other contexts.
The next problem I ran up against was how to deal with DbContexts within a plugin. The plugin may well need to use a "core" entity but also define its own.
Deciding to use a single DbContext for the whole system, using MEF and the solution to this question (MEF Plugins and EF CodeFirst - How?) I was easily able to manipulate the single DbContext to handle additional entities defined in the plugin.
All is working well at present, although I'd read about poor performance of large DbContexts - moving forward, depending on the number of entities and performance of the single DbContext I may well refactor to break up into a series of smaller DbContexts and have a single DbContext with everything in it for managing the database sync.
hope this helps someone with same question
I'm looking a way to use Entity Framework to build SQL queries to DB without using Entity Data Models.
The reason why I'm looking a way to use EF is it has database providers for MySql, SQL, SQLite and I wish I had opportunity to use any of them in my projects.
There is classes that implement DbExpressionVisitor< T > in every provider so I think maybe there is a way to construct some DbCommandTree objects to pass it to SqlProviderServices for example.
I want to achive almost the same functionality as dynasql or Kerosene ORM has but with good community support. I know I will need to create some proxy classes to use DbExpressions in fluent way.
Apart from the fact that it’s open source and mature, what are the differentiating factors that separate nhibernate from rest of the .net ORM frameworks like Subsonic, ADO.NET Entity Framework etc?
Better put, why should I choose nhibernate over other .net entity frameworks?
The biggest reason is probably that nHibernate supports persistence ignorance; your entities can be plain old CLR objects (no base class). It also supports unit of work (updates are automatically tracked and batched) which Subsonic does not support. Also, Entity framework does not support implicit lazy loading (when you want to access Order.OrerItems, you have call Order.OrerItems.Load()). It forces you to think about lazy loading, but also pollutes your buisness logic with infrastructure concerns.