Data for MVC 3 code first app - visual-studio-2010

There is no way on earth this is supposed to be this hard. For 30 hours straight I have been trying to populate a code first database with data. I have tried using a sql server compact instance and a sql server express instance.
I am working with Visual Studio 2010 sp1 and my sql server is SQL Server 2008 R2 (express). Firstly, any database created by the EF I cannot open with management studio. Any database created with the correct schema by management studio cannot be hooked up to the visual studio app. I was under the impression if I pointed to that file EF would hook up to it. The schema is correct because I exported it from an EF created DB. However, that does not work and I get an error that the database already exists.
After painstaiking adding data to a EF created db through the visual studio database tools with scripts, the site would not run, I got an error that the database log file was different or the file was corrupted.
If someone could please just tell me how this is SUPPOSED to work, I would appreciate it. The project is ready to go, I just need to load it with production data and get it uploaded, of course it was due yesterday.
Here are some more details if you need them:
I use this following line of code in the global asax code file when trying to create db's, and I comment it out when trying to connect to a db:
Database.SetInitializer(new storagePropertyInitializer());
A few of the connection strings I have tried:
<add name="storageContext" connectionString="data source=.\SQLEXPRESS;Persist Security Info=False;User ID=xxxx;Password=xxxxx;Database=irrStorage;AttachDBFilename=|DataDirectory|irrStorageV12.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="storageContext" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;Database=storagePropsDB;AttachDBFilename=|DataDirectory|irrStorageProperties.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="storageContext" connectionString="Data Source=|DataDirectory|irrStorageProperties.sdf" providerName="System.Data.SqlServerCe.4.0" />
Thanks,
John

Try the following to open the db in sql management studio if you have an sdf file.
How can I manage SQL CE databases in SQL Server Management Studio?
I realize it doesn't address the other issue but your data import may work this way. Note your website must not be running or the db will be in use. Detach when done working with it.

Related

VS2013 - How do I connect to local .mdf with SQL Server Management Studio?

I'm starting a new project and intend to use a local database file during development, rather than using a network server. I can query it by right clicking on the .mdf in Server Explorer and selecting New Query, but that's going to be a pain in the neck to have to copy all of my sql scripts over to it on a regular basis.
I apologize if my post is ambiguous - I'm having a hard time wording what I want to do. I want to use the SQL Server prompt with the .mdf file in my App_Data. (screenshot)
When I check the connection string of the .mdf in Server Explorer, I get the following:
Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\[username]\Desktop\...\App_Data\localdb.mdf;Integrated Security=True
I've not managed to get this string, in any form, to work, though.
Ran into this same issue and was getting annoyed. Solution ended up being quite simple. This is the connection string generated by Entity Framework:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\******.mdf;Initial Catalog=******;Integrated Security=True" providerName="System.Data.SqlClient" />
Simply grab the data source and plug that into the "Server name:" in SQL Server Management Studio:
Good to go!

MVC3 Music Store Tutorial Connection String issue

I'm working my way through the MVC Music Store Tutorial and am running into an issue when I try to connect to the database using the entity framework model. I've tried a number of these walkthroughs, and I'm continuing to run into problems when I get to this part.
I do not want to use SQL Compact Edition (although I've tried to install it just to get the tutorials to work). Rather, I have SQL Server Developer 2005 Edition as well as a named instance of SQL2008 Express (again, installed just to see if I could get the tutorial to work). Here is my connection string:
<connectionStrings>
<add name="MusicStoreEntities"
connectionString="server=2-BQZ5DP1\DELS2008EXPRESS;Integrated Security=SSPI;database=MvcMusicStore"/>
</connectionStrings>
The closing tag for the connectionStrings element will not post in the code snippet, so pretend that it is there.
What do I need to do differently? 2-BQZ5DP1 is the name of my box, and the SQL Express instance is a named instance.
You generally need MultipleActiveResultSets=True with Entity Framework when you're using SQL Server editions other than Compact. Depending on what the error you are seeing is, that might be your problem. For more on connection string options, see this blog:
http://blogs.msdn.com/b/aspnetue/archive/2012/08/14/sql-server-connection-strings-for-asp-net-web-applications.aspx
how about
ConnectionString="Data Source=.\DELS2008EXPRESS; Initial Catalog=MvcMusicStore; Integrated Security=SSPI;"
You could be missing the username and password from your conn string? Application and timeout are other parameters you might need depending on your set up. Check out http://www.connectionstrings.com for more help with SQL 2005.
Furthermore, Entity Framework requires many more parameters in the connection string. I have not used the code first approach yet but if you have existing tables you wish to map then EF has a wizard you can use to identify the database objects you want to work with. To get this far you have to provide a valid connection string to the dialog. In doing so VS will populate your web.config with the well formed and fully constructed conn string for use with EF.

Why is EntLib looking in my source code App_Data directory for my database instead of in SQL Server's normal location?

I'm trying to call a stored procedure from my MVC app using EntLib 5.0. Here's my connection string (for SQL Server 2008):
Server=local;Database=Test;Trusted_Connection=True
I'm getting this error:
An attempt to attach an auto-named database for file
C:\dev\pestsApp_Data\aspnetdb.mdf failed. A database with
the same name exists, or specified file cannot be opened,
or it is located on UNC share.
Why is EntLib looking in my source code directory instead of in the directory where my database is located (the default location)? Here is the actual location of my database:
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA
How do I make EntLib connect to that database?
In your web.config you'll have to put a in the beginning of your Connectionstrings section.
If you don't, your application will inherit the connectionstrings that are defined in the root web.config by default.
So:
<connectionStrings>
<clear/>
<add name="DefaultConnection" connectionString="Server=local;Database=Test;Trusted_Connection=True" />
</connectionStrings>
This issue is not related to EntLib 5.0.
Perhaps a typo?
C:\dev\pestsApp_Data\aspnetdb.mdf => C:\dev\pests\App_Data\aspnetdb.mdf
And your application can access C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA? By default it could not.

ASP.NET MVC 3, SQL Server: Tables not appearing in explorer, but they're there

My database is stored in an .mdf in App_Data and is functioning fine. My application can add, edit, delete records.
But the tables are invisible. When I open the Server Explorer and attach my .mdf and try to view the tables, there are none listed there. I attached the file to SQL Server Management Studio as well, but the only tables that show up there are those in the folder System Tables.
From what I've been able to glean from the technobabble on MSDN this could be a permissions or ownership issue. I don't know about permissions, given that I'm able to connect to the database and query it and edit/delete records. So maybe ownership; I read somewhere that tables not owned by dbo may not show. But if that's the case, I don't know what my application's ownership name is or how to make Management Studio or even Server Explorer show tables owned by other users.
Here's the connection string, in case the answer's in there:
<add name="EFDBContext" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=|DataDirectory|MLDatabase.mdf;Database=EFDbContext;User Instance=true" providerName="System.Data.SqlClient"/>
Thanks to anyone who might point me in the right direction!
ETA. this seems like a similar issue, but I don't know if the solution there would apply, since my database wasn't generated by scripts but by the Entity Framework (Code-First model).
I had a similar problem. I scripted a backup of the database, added it to mssql server and pointed my connectionsstring to the new one instead.
The reason the tables weren't showing up is because they were not in the .mdf, of course. As I noted in a comment, I found them elsewhere, in a location chosen by SQL Server.
A workaround is to just let EF build the SQL Server database where it wants, stop the SQL Server service, move it to the App_Data folder, change the connection string appropriately, and then attach it to the ASP.NET project.
Another issue is that SQL Server's Network Service does not have permission to do anything in the C:\Users\Me folder, or the Documents and Settings folder, until you go to that folder and assign permission to the Network Service directly. Then and only then will you be able, for instance, to attach the database to the SQL Server Management Studio. This is true even when Visual Studio installs SQL Server... and then denies it permission to access the files where VS keeps it's own projects. I don't get it; IMO any developer who's aware of this, the first thing she is going to do, just in order to get her work done, is to give Network Service that permission anyway.
Check the defaultConnectionFactory type in your config. I expect it is set to LocalConnectionFactory, as this seems to be default.
Change it to the following and your SQL instance will be used.
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True;" />
</parameters>
</defaultConnectionFactory>
Your DB should appears in SQL Management Studio (SQLMS) with a name that matches the namespace.
Please do not be concerned. it is simply necessary to re-configure the connection string in the Web.config file accordingly:
- Remove the "\SQLEXPRESS" postfix from the server name;
- Remove the "User Instance=true" key/value pair.
If Your DbContext Class is StudentDbContext Then put Database=StudentDb;
Or

Configure an ASP.NET MVC3 project using Membership Provider to work with SQL Server Compact Edition

I have an ASP.NET MVC 3 project that I just created using the project template that includes the Membership provider all set up in a default manner. I'm now trying to configure the the project to utilize SQL Server CE as a data source. I'm following this blog article without much success.
http://weblogs.asp.net/scottgu/archive/2011/01/11/vs-2010-sp1-and-sql-ce.aspx
Half way down the article you'll find the subheading "Walkthrough 2: Using EF Code-First with SQL CE and ASP.NET MVC 3", which is the portion that I am trying to follow.
According to my understanding, all I should need to do is to configure my connection string in the web.config file so that it will utilize SQL Server CE. And as soon as I run the application and try to create a new user, the CE database file should automatically be created in my App_Data directory and I should be up and running. Granted this walkthrough is doing things a bit differently and is not using Membership Provider, but I should be pulling the underlying concepts from this and still having success. Instead I am getting an error when I try to create a new user saying that it could not find the data source.
I'm using the following for my connection string in the web.config file...
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=|DataDirectory|AppData.sdf"
providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
What am I doing wrong?
The standard membership provider only supports SQL Server, but you can use the new Universal Provider available via NuGet: http://nuget.org/packages/System.Web.Providers

Resources