nickornotto
nickornotto

Reputation: 2156

How to create a new MVC project with EF6 Db first approach in VS 2015

I want to create a new MVC project with EF to use database first approach.

So I have created a new MVC 5.2.3 project in Visual Studio 2015 using a wizard which created default Account and Manage users models, controllers and views.

The connection string is the sort of

Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MyProject-20161112112119.mdf;Initial Catalog=aspnet-MyProject-20161112112119;Integrated Security=True" providerName="System.Data.SqlClient

And I have a few issues here:

  1. There is no mdf file within App_Data folder of my project so I have no idea where the hell the database went to.

  2. I guess the app uses Code first approach by default so obviously I would like to change it. An idea is to just copy the tables from within the internal project db created on set up and add them to my new database I will use for db first approach. But I can't find the existing app database (see point 1).

  3. I can connect to the server which is used in the connstring above ((LocalDb)\MSSQLLocalDB) via Management Studio but it has no databases at all not even the one mentioned in the conn string. And the file is not present within App_Data folder

  4. So I have created a brand new database MyProjectDb within my localhost server using Sql Server Management Studio with the intention to use it for db first approach.

  5. I have created a new project MyProject.Database within my solution to use the newly created database MyProjectDb.

  6. I checked and can see that the database string in MyProject.Database project properties has connstring datasource as (LocalDb)/ProjectsV13 instead of localhost. The question is why such strange source rather than my localhost? And how to force new db project to create a database within localhost server?

  7. Next I have modified the properties of the database project (Debug tab) to use in the connection string localhost as datasource and the new database MyProjectDb I created.

  8. Now I right click on my database project MyProject.Database: add > new item > Table. I created the table.

  9. I can now see the database in SQL Server Objects Explorer under Projects-MyProject folder as well as I can see the database MyProjectDb I created under my localhost server within SQL Server Management Studio. The former has the table I created, the later does not have it under Tables (I refreshed the tables of course). This is weird and I am confused whether they are actually the same databases or somehow VS copied the database instead of using the one I created?

  10. I right click my web project in the Solution Explorer: add > new item > Data > ADO.NET Entity Data Model, I give the name, on the next screen I am selecting EF Designer from database option. I click next and get to the screen where I can create New connection. I am clicking this and the only server I can select from the list is NICKO-PC server. (Remember: the db project I created in VS defaulted to (LocalDb)?ProjectsV13 server) If I try to insert localhost or anything else it does change back to NICKO-PC but I guess it is anyway the same as localhost server cos when I expand available databases my MyProjectDb database is on the list. So I select it and go next.

  11. I get Choose Your Database Objects And Settings screen. And now I can't see my table I created using my MyProject.Database project. There is no table to choose to be more precise. So obviously I can't create any db entities to use in my application.

I am stuck.

I hope my description of events is not confusing.

Can anyone clever help me to do what I want please?

  1. I don't know why the web project used db file on some strange (LocalDb)\MSSQLLocalDB server and database project creates it under (LocalDb)/ProjectsV13 instead?

  2. How can I create my own new database and use it for both web app and database project then create entities based on the db first approach?

  3. How to get the tables and other object in data entity wizard I want to create model entities within my app?

This is basically what I need.

Please let me know how you create a project using Db First approach cos I'm not getting anywhere with VS studio wizards.

Thank you

* EDIT *

My DbContext class is rather simple and created by MVC project I installed together with user models:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

It points to the default conn string as you can see which I changed to connect to my newly created db MyProjectDb.

Upvotes: 0

Views: 230

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239460

Once you actually run the site and hit something that requires database access. Entity Framework should create the database. I think you might have simply diverged off the path too soon.

Regardless, yes, the default will be code first, or perhaps more appropriately: the default will be to create an new database. The old "model first" and "database first" approaches have been deprecated, and while you can still use them for now, it's not recommended, especially when building a new app you'll need to support long term. That said, "code first", despite the name, can actually be used with an existing database or to create a new one. You'll just need to alter your context slightly to use an existing database:

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext() : base("ConnectionStringName")
    {
        Database.SetInitializer<ApplicationDbContext>(null);
    }

    ...
}

Then, edit your Web.config and add a connection string to the existing database you want to connect to. Ensure that the name you give it there, matches where "ConnectionStringName" is in the code above.

Upvotes: 1

Related Questions