1

I'm new to ASP.NET and I'm working on this project that uses MVC. It's a simple website development project, and it uses SQL Server CE (.sdf) as the database.

I would like to change a couple of the underlying models, but that causes errors in the database.

I understand that I would need to change the database, and I know it's possible to create models automatically from the database.

I'm wondering if there is any way to create a database from the models. i.e. I don't mind losing all the data, but is there a way for me to change the models and then create a database that supports the existing models.

I hope that makes sense

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Yes it possible to Here is how you can do it. There are two approaches to accomplish this task.

Method-1. (Via Package Manager Console)

This approach for the Code First.

Open your project and go to Package Manager Console as follows

Tools--> Nuget Package Manager--> Package Manager Console

First Enable Migrations in your project.Type following in console.

enable-migrations -contexttypename yourContextName

This command will add a Migrations folder in your project inside this folder you will see Configuration.cs inside this file you will see a seed method

protected override void Seed(YourContext context)

you can use this method to seed your database.

Now run following command in your package manager console

add-migration anyNameOfInitialMigrations

Finally To update the database simply run following command.

update-database

Method-2. Best approach for Model First. Open your .edmx file.

Right Click--> on the model and then select "Generate Database From Model"

Make Sure you have proper connection strings in your web.config.

Husrat Mehmood
  • 2,270
  • 1
  • 20
  • 22