Creating separate projects for entity classes ( POCO classes) and DBContext classes - c#

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.

Related

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.

How to resolve Namespace and data models name collisions?

I have a project built with ASP.NET webforms that used ADO.NET to communicate with the database. I recently transformed all the model classes using Entity Data Model Wizard. It created the model classes with same table names as in the database. Now I want to create API controllers but the problem is that I can't use these model classes as I have the namespaces with the same name as the tables so I get an error when I compile the code. Is there a way I can resolve this?

EF database first edmx extension

I am using a database first approach with sqlce 3.5 and entity framework. Next I am extending my generated (.edmx file) partial class with external partial class properties where I implementing business logic. These extra properties are not required to be stored in database. Is this correct solution to the problem or are there any other more adequate solutions ?
there is no black and white in general; in this case if you are using the partial classes properly so you add all your custom logic not to the auto-generated files from EF (edmx.cs...) but to other files in the same project, you can basically extend the Entities or the ObjectContext as you wish and you are free to regenerate any time when either database changes or you update the model in the designer.
I use this logic in general and more specifically I try to use the layering as I described here: https://stackoverflow.com/q/7474357/559144 and I make all layers except the DAL fully independent from the EF. hope this helps :)

RIA Services : expose services when entities are in a different project from EDMX?

My Visual Studio solution has the following architecture :
a "DataAccess" project, containing one EDMX with his "object context" class (but without any generated business class)
an "Entities" project, containing the business entities generated from the EDMX. (ex : Customer class, Order class, Product class, etc).
a "Web" project containing the ASP.Net GUI.
Now I want to add a small Silverlight area in my existing application, with RIA Services.
I don't succeed in creating the domain Service : the "Add new domain service class" wizard contains my ObjectContext but itself contains no entities. It seems that it's because the EDMX is NOT in the same assembly as my entities.
Now I don't want to rebuild my existing application with a new architecture by merging the 2 projects (actual architecture seems clean for me).
How can I make this %$$$! wizard see my entities ? Should I construct the domain service class manually ? How ?
Thanks !
EDIT : I'm using C# (4), EF 4, Silverlight4
EDIT 2 : my entities are generated with the "ADO.NET POCO Entity Generator".
Should I construct the domain service class manually ?
I reckon so. It's the same using EF 5 with POCOs, the wizard doesn't pick up the entities. There are some helpful snippets on Colin Blair's site for creating the CRUD methods over the DbContext.

EF 4.1 DbContext Generattor - Put Entities in different project?

As a part of our application architecture, we like to define clear lines between our functional layers. A typical application solution, therefore, will contain:
Entity
Model
Task
Presenter
FrontEnd
These end up being completely distinct assemblies.
The Entity/Model delineation is done to keep database access functionality in a separate layer from our POCOs, so that only Task ever need know about Model, while everyone up to Presenter knows about Entity
This works well when using Code-First or Fluent-API - but due to the lack of support for SPROCs in those paradigms, it turns out that under EF 4.1 I must use EDMX models.
So - I'm generating POCOs using a DbContext Generator, but the resulting classes end up under .Model, and while I can force their namespace into .Entity instead, they still live in the .Model assembly, which means now .Presenter must reference .Model to get to classes that should be in .Entity.
Is there a way to force or trick EF to dump its generated output into a different Project?
Sure. DbContext Generator are just two T4 templates. You can move the template generating entities to other project. You just need to modify template to point to correct EDMX file. This is default:
string inputFile = #"Model.edmx";
You must change it to relative address to your EDMX file. It will be something like:
string inputFile = #"../Model/Model.edmx"
The template will automatically use default namespace of current project for generated entities but you will have to modify the second template for context to use the new namespace so that entity types are correctly resolved from referenced assembly.
There is small disadvantage of using template in another project - it will not update automatically when you modify model. You must always trigger entity recreation manually by using Run custom tool from context menu on template file.

Categories

Resources