EFCore Scaffold : generate contexts inheriting from subclasses of DbContext - c#

I am using EF Core scaffold to generate my contexts and model classes.
For each DB, Scaffold generates a context class that inherits from DbContext class, which is quite logical.
Now, I would like to mutualize some features for all my DbContext classes into a MyDbContextBase, inheriting from DbContext.
And I would like Scaffold to generate the DB context classes inheriting from this MyDbContextBase, instead of inheriting from Dbcontext.
Is that possible?
I did not see any options that would help me to do it.
And I clearly do not want to update the Scaffold generated class manually everytime that I regenerate the model classes via Scaffold to change the base class from DbContext to MyDbContextBase.

Related

How to use entity framework dbcontext in data layer than in the controller

I've created a .NET/Angular project with Entity Framework.
Actually, I am with Entity Framework Core 7.0.1 in CodeFirst and .NET 6.0
I have this structure :
App.Controllers
App.Model
App.Business
I want to use the DbContext only in the data layer (App.Business) and be able to use my DbContext like this :
Documentation of entity framework for Include() fonction
Actually, the injection dependency inject the DbContextOptionsBuilder in the Entity Framework context class :
DbContextOptionsBuilder
And I can use it like this and like all the examples that I found :
Controller
The problem is that I found any documentation who explain how to use the DbContext this way.
All tutorial I Found use the DbContext in the Controller, and that's the same for the documentation by Microsoft...
So, how can I modify the injection of dependencies to the data layer and not the Controller ?
I've tried to use a IRepository classe and use it into the Controller but without succes.
I've tried to compare my code with other project on github but anything was like I want.
Thanks.

Auto generate entity model from database to current DbContext class

I am working on CodeFirst and have auto-generated the DbContext and Entities models for the first time.
It successfully generated Entity Models:
But for awhile, the Database has had some new tables, and I have had to generate new C# Entity models. Currently, there is no way allow me to automatically generate a new Entity class for the existing DbContext.
Would you please advise me on it?
You have three options (to my knowledge):
Delete DbContext and existing models and regenerate from scratch. If you haven't made any changes to the default context or entities this is an easy approach.
Generate a new model and copy and paste the DbSets and model configurations into your existing DbContext. Then you can just delete the newly generated context.
EntityFramework Migrations - Once you have the database created you can make future updates to your code-first model and then apply them to your database rather than the other way around.

How to dynamically create db tables with code first in a prism wpf modular app?

I'm using prism to build a modular app with pluggable modules.
I've already set up sqlite with EF successfully in a separate project to be included in each module.
BUT
I want my module to have a "Model" folder with classes required only by it, in a way that, when loaded by the wpf shell, the system will create db tables for it (if they already don't exist).
How can I "inject" my model classes into the DbContext class at runtime like when I register views and viewmodels with UnityContainer?
T4 (Text Template Transformation Toolkit) is a tool which you can use it to generate DbContext automatically from your Model classes.
I will not write the whole code example because we have already a good example in the given link below:
https://www.paragon-inc.com/resources/blogs-posts/using-t4-to-generate-a-dbcontext-in-code-first
How can I "inject" my model classes into the DbContext class at
runtime like when I register views and viewmodels with UnityContainer?
T4 idea here is similar, you generate your DbContext with all DbSets from the existing classes(Model):
This apporach will extend the DbConext and adding the "Model" folder with classes dynamically(Reflection) in Complie Time.
1) With T4 generate your DbContext class
2) With reflection load your Model entities (retrieve the model class names which you can use them to create the DbSets in the step 3)
3) With T4 generate the DbSets from the loaded entities(step 2) and adding them to the DbContext
[Optionally]
If you want to generete the entities and the DbContext with T4 then, you can take a look to this project.
https://github.com/coni2k/DbContextGeneratorWithCodeFirst
if you use EF 6 you can try modelBuilder.RegisterEntityType(type); for adding new Entity in you context and you can handel migration configuration to add your entity. Also you can use modelBuilder.Configurations.AddFromAssembly(assembly); to add your Entity configuration to your context.
i have a modular web application in this way.
when my web application is started i check all modules and added the entities and shared entity to a context (BTW. some of modules have there own context) and migration do our changes to main database.

Creating separate projects for entity classes ( POCO classes) and DBContext classes

I have generated my Database layer (Database First Entity Model) by using entity framework 6.1. Now all the classes generated are in the same project as well as in the same namespace i.e. Example.DataAccessLayer. For separation of concerns, I have moved my entity classes(POCO classes) that are under Model.tt file to separate project and under namespace Example.DataModel. The reason I have done this is because then I can use Example.DataModel project in my websites so that the DbContext classes are not visible and all the data management is done through my business layer i.e. Example.BusinessLayer.
Now doing this I have to give reference of my Example.DataModel project to Example.DataAccessLayer. As it is suggested in some of the tutorial, after doing this one has to change the Custom Tool Namespace of MyModel.Context.tt file to Example.DataModel so the entity classes are visible. But by doing this the DBContext and DataModel comes under same namespace that is Example.DataModel.
Now the question is, is there a way to generate my entity model with my context and entities classes in separate projects and in separate namespace without giving my DBcontext.tt file custom tool namespace ?
Why not use Code-First to connect to you existing DB? Then you have full control of where Models and DbContext lives.

Entity Framework generated types not inheriting from EntityObject

I'm new to Entity Framework (I'm using EF5), and I'm working on a test project to learn something about it.
Now I'm implementing some DAO classes to access DB following this tutorial (it's a bit old...)
I generated entity classes using Entity Data Model tool, but now I need all entities to inherit from EntityObject class.
Reading this I expected it to be so, but actually my classes inherit only from IObjectWithChangeTracker, INotifyPropertyChanged.
So I'm wondering if I'm doing something wrong...
Do I have to set some configuration in generation tool?
You need to use the correct template from http://msdn.microsoft.com/en-us/data/jj613116.aspx - one of those that are called "EntityObject Generator".

Categories

Resources