How to create new base line / reset in EF code first? - entity-framework

So when prototyping an application using Entity Framework code first, I ended up with a lot of migrations.
I'm not interested in keeping all the classes that were generated, I want to say: ok, this is my base line for v1.
How can I reset EF Code first?

If you want to return the first migration in your database you needs just:
Update-Database –TargetMigration:"YourFirstMigrationName"
in your parkage manage console.
But it just return the version of your database, not your code first POCO classes. For control the version you need another tool, like the Team foundation or git.

Related

EF core DataBase Update

Guys we moved Framework 7 to EF core 2.0 .So right now we have a Small problem.
when We use Entity Framework 7 its mostly easy to update client Database without any doubt.(update -database)
but in EF core there is a problem the reason is for every changes we have to add add-migration so in that case we have now 100 migration history.
example :(20180313063924_NewVersion,14689013063934_NewVersion etc)
so when we update client database we have to keep that 100 migration history
But i think this is not the good way when its come to production level
is there anyway to resolve this problem.it would be helpful so much thank you!!
Well, it is exactly the way like EF and EFCore are working.
Every migration represents the needed modification on DbContext/Database to be valid with model's changes. So if you have changes, they will be represented by a migration.
One - in my opinion - not very clean solution could be:
delete current database
delete whole Migrations directory (is valid too to delete all migration files and <yourContextName>Snapshot.cs file in Migrations directory)
add new migration e.g. InitialCreate
The result will be only one migration that represents your current project's model/dbcontext state.
The approach is only possible if the project is still in dev-phase without any deployments on any stages.
Please note, I don't recommend that solution/approach. In my opinion you should leave the migrations like they are.
For further information you should read following:
The Model Snapshot In Entity Framework Core
Migration in Entity Framework Core

EF 6 Mix Mode Code First and DB First

Iam using EF 6 with database first .. and every time I update the scheme : all my editings to the edmx file (mainly for defining db built in functions) are lost .. but using code first I can add my dbmodelbuilder calls in a separate file with partial class .. and by that I can also use all the nuget packages that targets code first such as EntityFramework.Functions.
Thanks for helping
You can use Code First from database option when you are creating your ADO.net entity data model. This will give you the code first classes that you can edit.
After creating your model you would need to stick to using the code first model otherwise you will need to regenerate your code-first after every database change...
Download the EF tools here.

Entity Framework Code First manual database changes not updating

Maybe I'm defeating the purpose of Code First but for one reason or another let's look at the problem.
I'm working in EF Code First. My 'Cars' POCO has a "Make" field,.. that matches the "Make" column in the db. Now I come along and I manually rename the column in the db to "Manufacturer". How can I force EF to catch up and update/ rename the POCO?
At the moment I'm using EntityFramework 6.1.3 and VS 2010.
The only solution I could find to this problem of the database being out of sync with EF on the code side was to delete all of the MIGRATIONS and the ENTITIES poco classes, and on the db side delete the _migrations table. Then go back to your application and add to your project a new "Code First from Existing Database".
This solved the problem for me easily and will allow me to work with Code First AND SQL Server Management Studio (as I like to do (until I get better at EF)).
I know it's a bit of a work-around but it'll get you out of jail if you find yourself in this situation.

Entity Framework 7 and EDMX manipulation to auto-generate custom code

I currently use EF6 and use the model first approach. As I understand it, EF7 will be moving away from using an EDMX, and going from a more code-first approach. Now I know I will still be able to reverse engineer from my database into classes if need be.
However one thing I am not sure about is any manipulation I currently do with EF6 will be supported in anyway in EF7.
At the moment, I write T4 templates that read through the EDMX, pick up on the entities, and create new classes based on them. For example, I create partial classes for each entity that has deep clone methods in them. I also create repository classes based on the entities and create methods for finding by primary key, based on which properties in each class have been identified as the primary key.
If I lose the EDMX, does this mean I need to go back to manually creating these? Or is there another way?
If you want to keep using T4 templates, you can switch to something like CodeFirst -> ReverseEngeneer approach.
You update model in code, generate new migration, test it on a database and then use a reverse engeneer code first approach (http://msdn.microsoft.com/en-US/en-en/data/jj593170.aspx) to generate everything else. Theoretically it can be automated.
In my team we do it manually, but we do not need migrations, only a code first contexts and a lot of additional things, that T4 generates whery well.
Yes, you can still use T4 templates with Code First, We navigate Entity Classes instead of the EDMX Model, .
I have been looking at VS2015 recently and having some issues with T4 and asp.net 5 and related projects (FileManager hangs for multiple file outputs and you will need the latest version of Visual Studio, currently Update 1)

Can I add the PluralizingTableNameConvention to the EF powertools?

I am currently using the Entity Framework Powertools (beta 4) to generate my POCO classes from an existing db, which has upwards of 800 tables.
Whilst this is awesome, and saving me a lot of time, I noticed that the tool is pluralizing my POCO classes, or de-pluralizing them. Some of our tables are pluralized, and others are not, so to keep things simple, I want the POCO's to match the underlying db table names. In my main DbContext, I have the PluralizingTableNameConvention and PluralizingEntitySetNameConvention removed in the OnModelCreating() method call, so the application is fine.
My question is whether this configuration can be re-created in the EF Powertools so that the classes come out correctly, and do not require me retrofitting the class names etc.
It looks like this cant be done, however I have download the EF Powertools code from Codeplex, and built the tools manually. I then created my own implementation of the PluralizationService in which I did not pluralize anything.
Job done.