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 :)
Related
In my recent interview, my interviewer we asked about approaches in entity Framework I told them code first and table first. Is there any approaches pending.
We can use three type of entity framework approach as per project requirement.
Database First:
An existing database can be used
Code can be auto-generated.
Extensible using partial classes/ T4 templates
The developer can update the database manually
There is a very good designer, which sync with the underlining database
http://www.entityframeworktutorial.net/database-first-with-entity-framework.aspx
Code First:
There is full control of the model from the Code; no EDMX/designer
No manual intervention to DB is required
The database is used for data only
http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx
Model First:
Good support with EDMX designer
We can visually create the database model
EF generates the Code and database script
Extensible through partial classes
I'm in the process of converting an extensive EDMX model into POCO classes. I need to go from a Database First approach (EDMX with ObjectContext) to a pure Model First approach (DbContext with no EDMX file). I need to use the latest Entity Framework stable version: 6.1.1.
I've tested some approaches:
Adding a the EF 6.x DbContext Generator code generation item by right-clicking the blank space in EDMX designer. This works fine, but it doesn't add any mappings. With this approach I have to still use the EDMX file. It's not full Code First.
Using the EF 5.x DbContext Fluent Generator for C#. This triggers an exception in design time. I'm not being able to use it. I don't know if that's because my VS Entity Framework tools are already updated to 6.x. Using the alternative TT in the comments, that suggests that it would work with EF 6.0 also doesn't help.
Using the EntityFramework Reverse POCO Generator. This is the worst because it won't consider any of my classes and navigation properties renames.
Using the Entity Framework Power Tools Beta 4. Again, it only supports generating from the database, not from the EDMX file.
My requirements:
I need the input to be the EDMX file, not the database.
I need the output to be a full Code First approach with Fluent mappings.
I need all my navigation property names defined in the EDMX to be considered because otherwise it would break a large codebase, even more then migrating from ObjectContext to DbContext will break.
What do you think would be a good option for me to go?
Well i don't think there is an easy one click solution to this.
Underneath you edmx files. You have two more files available besides the xx.Designer.cs and xx.edmx.diagram.. called xx.Context.tt and xx.tt where xx is the name of your edmx model.
These are t4 templates which genrate your dbcontext and poco objects. All your poco objects would be created underneath your xx.tt files and dbcontext underneath your xx.Context.tt files.
You now have to moves these into separate files. This is much easier if you are using EF6. and the file generated are already using DbContext and not ObjectContext.
I faced a similar case and I used Entities to DTO's generator.
Although its purpose is to generate DTO's, however, I believe it can help someone in you case.
https://entitiestodtos.codeplex.com/
Let's say I have a project where I use Entity Framework, but I want to use my own classes instead of the EF classes.
Reasons for using my own classes:
Easy to add properties in code
Easy to derive and inherit
Less binding to the database
Now, my database has table names like User and Conference.
However, In my domain project, I also call my files User.cs and Conference.cs.
That means I suddenly have two objects with the same naming, which is usually very annoying to work with, because you have to use namespaces all the time to know the difference.
My question is how to solve this problem?
My ideas:
Prefix all database tables with 'db'. I usually do this, but in this case, I cannot change the database
Prefix or postfix all C# classes with "Poco" or something similar
I just don't like any of my ideas.
How do you usually do this?
It's difficult to tell without more background but it sounds like you are using the Entity Framework designer to generate EF classes. This is known as the "Model First" workflow. Have you considered using the Code First / Code Only workflow? When doing code first you can have POCO classes that have no knowledge of the database, EF, or data annotations. The mapping between the database and your POCOs can be done externally in the the DBContext or in EntityTypeConfiguration classes.
You should be able to achieve your goal of decoupling from EF with just one set of objects via code first.
To extend the above answer, the database table name User (or Users as many DB designers prefer) is the identifier for the persistence store for the object User that's defined in your code file User.cs. None of these identifiers share the same space, so there should be no confusion. Indeed, they are named similarly to create a loose coupling across spaces (data store, code, development environment) so you can maintain sanity and others can read your code.
I have added added Entity Framework to my project and selected Code first from database when creating my models. But the problem is, i couln't find how find how to update existing models and add new models to my project when i make changes on database.
It is very straightforward. Switch on migrations, change/add you classes, create a migration and update your database. See here for example. There are plenty of other sources.
When you change your DB you can run the EF generation again but it will overwrite the existing files so you will lose any changes. If you want to maintain code outside the generated files then you can use partial classes. Or alternatively just code them by hand after the initial auto generation, it's quick once you get used to it! :)
I have an existing database that I want to generate its POCOs but I want to end up with model classes outside .tt file, without giving partial definition and "auto generated" comment header for each class I want to have a result like I started building POCOs from scratch. Is there a process to get this result ? Regards
I want to regenerate my database and start updating model from POCOs using migrations
Update : After editing Template File (.tt) I could remove the header comment and partial definition. I think I could get my work done manually. Now, I just need to Exclude .tt file and its sub classes from project and then add only my business classes to the project again.
You can use Entity Framework Power Tools Beta 3 extension to generate your pocos. It has an option to reverse Engineer Code First which Generates POCO classes, derived DbContext and Code First mapping for an existing database.