Where is my database in Visual Studio 2010? - c#

I'm creating my first project in (C#) ASP.NET MVC3 using Visual Studio 2010 Professional, I'm creating a very basic blog system. During the tests I created a some tables and now I want do store different data on those tables but I keep getting this error:
The model backing the 'CategoryContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.
Obviously I need to update the tables in database but I can't find where I can access the tables. I can open Server Explorer using CTRL+ALT+S but there is no Database or tables there.
Here is the screenshot of it:
http://img710.imageshack.us/img710/8944/screenshot001kd.png
Where/how can I find and/or edit the actual database tables?
Thanks.

Well here is the best I can do for you:
If using SQL Compact Edition then you should look into your project folder on the filesystem and look for a folder called app_data and you should have a database in there.
Alternativly you could just open your web.config file and look for the section that is the "ConnectionStrings" section and just look for the location of the database.
If it is in SQL Server Express, then in your Database Explorer window, click on the database connections and create a connection to your local sql server express. You should see it in the list of databases as the machinename\sqlexpress.

From your error I'm going to guess that you're using Entity Framework's code first approach.
What happened is that Entity Framework has created a database for you on the local sql express install. To find the actual database, look in your web.config file and find the connection string section which will tell you the server and the catalog for the database. You can either us Visual Studio to access the database by using the data connection area found in the server explorer panel in your picture. Or you can use Sql Server Management Studio to do the same thing.
Now the reason why you're getting an error is because Entity Framework created that database based on the entities which you initially defined. Once you have made changes, the database no longer matches the entities and you get the error listed above. Your solution is to either delete your database once you have found it and allow the auto-magic to happen again, or you will need to do some changes to your code to not auto-generate your database each time you make scheme changes.
I would suggest you look through MSDN's ADO.NET Entity Framework 4.1 and read up on 'code first' development styles.
Good luck, and hope this helps you some.

Related

Entity Framework with Linked Database and SQL Views

I am trying to use Entity Framework 6.1.1 in Visual Studio 2013 with C#. I am using SQL Server 2012 in the back end. I have tested a regular SQL Server database which works fine. I used the Database First design pathway. I have a database that is actually a Linked Server in Microsoft SQL. The back end of the linked server is SQLite. The linked server is interacted with via several views in a regular Microsoft SQL database. I do not have to write or modify data in the linked server in any way.
I cannot get Database First to build a model of this database with the views, regardless of what I try. I have tried to use Code First to manually write an interface with the database, without any success.
Is it possible to get Entity Framework to talk to this database? How can I do so?
Okay, if anyone later finds this and has the same problem, I resolved this by using Code First and manually creating Entities with names to match the SQL tables and variables with names to match the fields. This worked fine and I could even use navigation properties and all worked great.

Moving Entity Framework projects

I have developed a project using entity framework on my local machine. What I am wondering is what is the best way to move the project to live server. Do I need to recreate the entities on the live server, if tables are same? Or I need to change the connection string? Reason why I am asking is that on live server I might not have access to the Visual Studio to re create entities.
I searched around cant find what I am looking for. Appreciate your comments.
You need to create the database and objects on the server manually. You could export the DB schema to a SQL script through Management studio.
Also ensure that you get rid of the DropCreateDatabaseIfModelChanges strategy, which might be the case on your development environment- especially if you are using rapid prototyping.
If your connection string is externalized to web.config, donot forget to change that as well.

How To View Database Diagram?

I am working on a project which requires me to write several POCO classes using Entity Framework Code First. There is a lot of entity relationships and inheritance going on and its hard to keep track of everything just looking at the code. Now, as we know, Entity Framework Code First yields an .mdf file as your database, and i was thinking for verification, a database diagram would server me better.
Is it possible for me to view my database diagram in this scenario, and how may i do so??
You could always point it to a SQL Server Express database - by default an MVC 4 project uses LocalDB but if you're more comfortable in management studio you can always create your own database and change the connection string to that.
Also from memory I believe you can also attach an mdf file in management studio but may have trouble while the application is running. But I could be thinking of something else there.

Entity Framework & SQL Server 2008 Express

I'm totally new to Entity Framework and have done some reading, and as a test I have put together a very brief test framework with just two small Entities. I then right clicked and selected "Generate Database from Model" which takes me to the SQL Connections page. However, none of the previous connections I have used appear in the drop down list, and when I select Create New Connection I only have the options to use 'SQL Server Compact 3.5' or 'SQL Server Database File'.
I have come across this before with SQL Express and the work around is to create my own Connection String to access the required Database. However, with me using Entity Framework to create the database, it is impossible to write an appropraite connetion string.
I therefore seem to be in a Catch 22 situation.
I cannot write a connection string until the database is created.
I cannot create the database from EF without accessing SQL Server (via a connection string).
Anyone come across this or can point out what I'm doing wrong. Like I said I'm totally new to EF so I apologise if this is a very basic question.
Unless you want to use a database file in a user instance, you need to either use SQL Management Studio Express, or use the SQLCMD command line tool to create the database. I would recommend SQL Management Studio Express as it is easy to learn in my opinion.
The Entity Framework tools are intended to be used to create the database schema, not the database itself. You still need to define the file groups, security information, and other basics of creating and configuring a blank database.
See this Q/A for appropriate links:
How to create DB in SQL Express using SQL commands?
Thanks #psuedocoder, your assistance helped me resolve the problem. The answer in the end was simple, but not intuitive to me, hence my difficulty. I thought some elaboration on the answer might help others who are equally new to Entity Framework as me.
From Visual Studio
Create your EF model in Visual Studio.
Right click the model canvas and select 'Generate Database from Model'.
You are then asked to select the database connection. Select 'New Connection'.
In the 'Add Connection' dialog box, rather than 'Browse' for an existing database, just simply type the name of your new database in the Database text box and Click Connect.
When you try to connect you will get a warning saying the database does not exist, but you will be asked if you want VS to 'Create It'. Select Yes.
As #psuedocoder states this does not actually create the database in SQL Server, but it does create an object in your VS soluton which contains the TSQL script required in order to create your database.
From SSMS
Go to SQL Server Management Studio and create a blank database of the same name used in step 4 above.
In SSMS select open file and navigate to the windows folder containing your VS solution files.
Open the TSQL script file. This will have a DB Script icon and have an .edmx extension.
Click Execute, and you will have your new database created from your EF model.

Entity Framework 4.1 Code First Freeze

I'm having trouble getting EF 4.1 working on my computer. It seems to be some problem with my database settings. I was trying out this simple walkthrough:
http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx
But when it reaches db.Categories.Add(food); it just freeze.
I have a normal SQL Server 2008 R2 installed, not SQL Express. There also seems to be some problems with creating .mdf files instead of a direct connection to the localhost SQL server.
I've also tried adding an entity model with a database connection, but this does not seem to work. Do anyone have any pointers for me.
Thanks for any answers :)
edit:
I now get a System.Data.ProviderIncompatibleException with "Vendor returned no ProviderMaifestToken-string"
I guess this is because Database Re-Initialization.
If your EF code try to drop and create database with SQL Management Studion open or connected.
This situation comes up.
I found the problem. These tutorials do not contain information about some vital adjustments that I needed to make it work.
1) Your application must have an App.config containing a connectionstring named with the same name as your class that inherited from DbContext. In my case "TestEF_CF.ProductContext".
2) The database cannot be created before you start to use it. Just set the Initial Catalogue to the name you want Entity Framework to create when it autocreate the database.
Once I did this, it worked properly. I got further with the first step, but got an exception when I tried to save to the database. When EF autocreated the database everything worked fine.
One thing that may have made it more problematic on my development environment could be the fact that I cannot use local mdf files directly. Still it would not hurt if the EF team could share these details more open than they do now, it would save me (or us) some frustration.
Try setting the datasource on the connection string to .\SQLEXPRESS
It did work for me

Categories

Resources