2

I am new to ASP.NET MVC but have worked with RoR quite a bite so I am familiar with the MVC pattern.

I am curious about the possibilities when handling the database part when using the Code First approach. I was told by an ASP.NET developer colleague that you can create an empty database, that is a database with no tables and use EF Code First to create the tables in that empty database.

Is that correct?

I cannot find any tutorials/documentation on that particular approach anywhere. I have come across Code First approaches that start with an existing database and tables but not an existing database without tables.

I would love to know:

If this is possible. If it isn't, what's the next-closest approach?

Thanks

Thanks

2
  • You don't even have to provide an empty db. EF will (re)generate one. Commented Jun 20, 2013 at 20:21
  • So should I delete the current database then? Commented Jun 20, 2013 at 20:26

3 Answers 3

2

At it's most basic level, you create a class to represent your table, a context class (derived from DbContext) and then execute commands in the Package Manager Console to create migrations/scripts and/or update the database schema directly (think of it as rake db migrate for EF Code First).

public class User
{
    public string Username {get;set;}
    public string Password {get;set;}
}

public class DatabaseContext : DbContext
{
    public DbSet<User> Users {get;set;}
}

Tools -> Library Package Manager -> Package Manager Console

Once the console window is open, make sure the project with your entity/context is selected in the "Default Project" dropdown at the top of the window and execute the following commands:

Add-Migration "Description of your migration"

This will create a migration class for you that represents your changes to the database since the last migration. If this is the first one (given that you're starting from an empty database), it will also generate a system table called "__MigrationHistory" which is used to track which migrations have already been applied to the database.

Update-Database -script

Executing Update-Database with the -script flag will generate a SQL script you can save/check in to source control if needed. If you'd prefer just to update the database itself without the script, just leave off the -script tag.

Be sure you have a connection string in web.config named the same thing as your context class ("DatabaseContext" in this example). This can point to your existing database but it will only manage/track changes to entities which are defined in your context class. I've found sometimes it's necessary to explicitly define the context when using the Add-Migration and Update-Database commands like so:

Add-Migration "Description of your migration" -connectionstringname "DatabaseContext"
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Scott. One follow up question: I just ran those commands and it looks good but I don't see the new table in the database. Should I be seeing the new table?
If you ran 'Update-Database' without the -script flag then yes, you should see the table in your database. If you don't, try the -connectionstringname flag to be sure you're pointed at the proper connection (and not, for example, LocalDb or something).
Yep, that worked once i removed the "-script" flag. Thank You Much
2

Yes, it is possible by design. Here is the official tutorial

http://msdn.microsoft.com/en-us/data/jj591621.aspx

See section "Building an Initial Model & Database"

Note: If you database already exists, you can either name your dbcontext class as that database, or specify "Initial Catalog" in connection string to match your database.

1 Comment

Thanks Alex but that section that you referenced shows how CodeFirst creates the database and tables. I already have a database. Snippet: "Run your application and you will see that a MigrationsCodeDemo.BlogContext database is created for you. "
0

You can simply create a class wich that is your table like

 public class Person{
public Guid id {get;set;};
public string Name {get;set;};
}

then you can add a ADO.NET data entity model type codefirst empty database then add the class into that database

add a ctor in db

static myDB(){
Database.setInitializer(new DropCreateDataBaseIfModelChanges<myDB>());
}


public virtual dbset<Person> People {get;set;};

after that your database will be create

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.