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:
There is no mdf file within
App_Data
folder of my project so I have no idea where the hell the database went to.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).
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 withinApp_Data
folderSo I have created a brand new database
MyProjectDb
within mylocalhost
server using Sql Server Management Studio with the intention to use it for db first approach.I have created a new project
MyProject.Database
within my solution to use the newly created databaseMyProjectDb
.I checked and can see that the database string in
MyProject.Database
project properties has connstring datasource as(LocalDb)/ProjectsV13
instead oflocalhost
. The question is why such strange source rather than mylocalhost
? And how to force new db project to create a database withinlocalhost
server?Next I have modified the properties of the database project (Debug tab) to use in the connection string
localhost
as datasource and the new databaseMyProjectDb
I created.Now I right click on my database project
MyProject.Database
: add > new item > Table. I created the table.I can now see the database in SQL Server Objects Explorer under
Projects-MyProject
folder as well as I can see the databaseMyProjectDb
I created under mylocalhost
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?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 createNew connection
. I am clicking this and the only server I can select from the list isNICKO-PC
server. (Remember: the db project I created in VS defaulted to(LocalDb)?ProjectsV13
server) If I try to insertlocalhost
or anything else it does change back toNICKO-PC
but I guess it is anyway the same aslocalhost
server cos when I expand available databases myMyProjectDb
database is on the list. So I select it and go next.I get
Choose Your Database Objects And Settings
screen. And now I can't see my table I created using myMyProject.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?
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?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?
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
.