Reputation: 3584
I'm creating a Asp.Net Core project. The project came with a file for migration. This file will generate my DB. But I have some tables in a DB and I would like create the migration file like in the sample.
Sample:
migrationBuilder.CreateTable(
name: "AspNetRoles",
columns: table => new
{
Id = table.Column<string>(nullable: false),
ConcurrencyStamp = table.Column<string>(nullable: true),
Name = table.Column<string>(maxLength: 256, nullable: true),
NormalizedName = table.Column<string>(maxLength: 256, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetRoles", x => x.Id);
});
If I have an existent Table. How can I create this file?
Upvotes: 0
Views: 2284
Reputation: 1506
MigrationBuilder
only exists in EntityFramworkCore. Be careful of the difference between EntityFramworkCore and EntityFramwork 6.x. The documentation of EntityFramworkCore is here. The tutorial that may solve your problem is here.
Run something like this:
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer
to reverse engineer a model from the database. This will also generate model snapshots and migrations(not so sure, if not run Add-Migration yourself) for you.
Add-Migration
and Update-Database
Now you get the migration files and your models and the database are in sync.
Also, it is worth to have a look at the Feature Comparesion to get a feeling of what EntityFramworkCore does not support. Until now,
EntityFramworkCore doesn't have many of the GUI tools that EntityFramwork 6.x has. For example, you cannot use Entity Data Model Wizard that is mentioned in the other answer. If you use the GUI tools by accident, it will generate EntityFramwork 6.x related stuff by default. So, you have to use cmdlet
in the Package Manager Console in Visual Studio do such tasks.
Upvotes: 1
Reputation: 178
First you need to decide on which approach you wanted to go DbFirst or CodeFirst. The files can be generated automatically and then you can also make changes to the file manually.
I would recommend using the CodeFirst because it provides max control over your models and dbcontext.
Step1 : Create a codefirst model using the existing database.
Step 2: now type enable-migrations in the package manager console
Step 3: You use the (db)context and the model generated from the database table. make changes to your model
Step 4: type add-migration [some name to identify the migration] in the package manager console
Step 5: check the generated migration file.
Step 6: type update-database in the package manager console
Now your changes are updated to the database. From now on you can us the codefirst approach to handle the changes to your database.
Hope this helps!
Upvotes: 1