Generate POCOs from an existing database EF 5 (with some conditions) - c#

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.

Related

Create additional class using Entity Framework (Code First From Database)

I created a C# data layer using Entity Framework 6 via the Code First From Database wizard which defined all of the C# classes which represent their respective tables.
There was a table that I missed and I'm looking for a way to generate another class to represent the table.
Is there a way to have this class created after the initial wizard has already been run?
The reverse engineer tool is meant to be used once in code first. Once it has been used you would just add the class that represents the table along with the other entities. If you are adding tables in the database after you have used the wizzard then you are not truely using code first. Just add a POCO class to the entities folder the tool generated and model it after the others. Once you have done that you use the package manager console to add-migration which aligns it back with your schema, next you use update-database to push the changes back to SQL Server.

Regenerate Entity Framework Code First Models

Using Entity Framework 6 Code First in an ASP.NET project with Visual Studio 2013, is there any way to rebuild (or update) the generated DataContext and model classes without stepping through the Entity Data Model Wizard every time?
I'm fully aware of how to do this with an EDMX designer, but again, I'm using the "Code First from database" method and just wondering if there's a one-click (or one console command) way to trigger the rebuild without having to delete the generated context class and then step through the Entity Data Model Wizard every time I make a change to the backing database.
In VS 2015 (and supposedly 2013/2012) you can use the Entity Framework Reverse POCO generator to accomplish this.
https://visualstudiogallery.msdn.microsoft.com/ee4fcff9-0c4c-4179-afd9-7a2fb90f5838
You can make all your changes to database first, and to re-generate your models all you have to do is save your Database.tt file (usually I just add white space).
Code first requires you to create the DataContext by hand. You don't create/change the database and refresh the DataContext class. The Code first from database or EF Reverse POCO template is a middle ground between true Code First and the Database First approach of doing things. It meets at the middle by generating the same kind of POCO classes that you would have written by hand in Code First. They don't do it in Code First way but the end result is something similar to Code First. Hence the confusing name.
If you are using one of these templates to generate POCOs, you can right click on the t4 template file and click Run Custom Tool. If that doesn't work, you might want to delete that entity and then run the custom tool again. Also right clicking on the EDMX and clicking 'Update Model from Database' should work.
I have the solution for rebuild without wizard:
using the t4 is Transform All T4 Templates
and create the classes

How to update EF models when using DB Code first?

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! :)

How do you modify your database after generating your DbContext?

Now that i have generated 19 code-first classes from an existing database by:
Creating a Model-First Entity, and choosing "Generate from database"
Right clicking and choosing "Add Code Generation Item"
EF 4.x DbContext Generater
All the classes i want are generated, and I'm very happy with them...
And now for my question(s)
Why are they all the generated .cs files nested in a sealed .tt collection?
Why can't i copy paste them out to any folder i like, and treat them like normal classes?
Is it just me, or are you unable to use migrations "update-database" when you do it like that?
I guess my overall question is, why are they located in a .tt folder?
And how shall i update my database now? By editing the database manually and then update the Model, and then generate the dbContext again? Or is there a trick to get .cs files out of a .tt folder?
How do you modify you database after generating your DbContext?
Why are they all the generated .cs files nested in a sealed .tt collection?
Your code-first classes are not code-first classes. They were generated based on your database schema. Hence, this is a database-first approach. With database first, your entity classes are generated based on the database. In other words, the database comes first, the code comes second. The .tt file is a T4 template that decides how to generate your classes based on the database. You could change the database and then regenerate your entity classes.
Why can't i copy paste them out to any folder i like, and treat them like normal classes?
Because they are generated files, based on the T4 template.
Is it just me, or are you unable to use migrations "update-database" when you do it like that?
This part of your question I can't answer for certain, and will have to defer. However it seems to me like you want to mix and match approaches. It sounds like you want to start by generating code classes based on your database, and from there forward, change the database based on new entity classes you add to the model project. Is that right?
You can forgo the code-generation part and just go with a pure code-first approach, now that you have an idea of what the entity POCO's should look like. I have tried this T4 code generation before (pre EF 4.1) and immediately abandoned it, because as you have seen, since the entity and DbContext classes are generated, you lose control over the object-oriented model.

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 :)

Categories

Resources