MVC3 Music Store Tutorial Connection String issue - asp.net-mvc-3

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.

Related

Linq DataContext.CreateDatabase on Azure

This came up once before: Use DataContext.CreateDatabase in SQL Azure
The answer accepted was "maybe it's not possible". Didn't seem like a full answer.
I have a set of classes fully defined and I am wanting to create a database on Azure for this. It's not working because the USE statement does not work: http://msdn.microsoft.com/en-us/library/azure/ee336288.aspx
So, the database gets created as blank, and internally Linq generates a USE statement to move to that database and start adding tables. This fails and it throws an exception.
So how can I create my database? Can I use Linq to add tables to an existing database? Can I enable USE on Azure somehow? Seems ridiculous this does not work.
After messing around for a while on this, I ended up creating the database against a local SQL Server instance. Then used SQL Server Management Studio -> Tasks -> Script Database, and turned on the export type to be Microsoft Azure. Then I had the script file needed to run on the Azure server. I'll leave the question open for a day or two because I am curious if this can work with Azure directly somehow. If I don't hear anything, I will close it.
The USE statement does not switch between databases in Azure SQL Database. You will have to connect to the database to create a table on that database.
Regards
Dhruv

How do I view the database that EntityFramework created for me?

Just starting off with MVC 4 and Entity Framework 5 (4.4 for .net 4). I've defined a couple models and saved something to database. I want to see what's going on behind the scenes, but I can't figure out how to connect to this database. How do I do it?
I assume I can connect to it via the Server Explorer somehow, but I'm not sure what options to pick.
You can get the connection string using DbContext.Database.Connection.ConnectionString - it will tell you everything since it is used to connect to the database. Then in server explorer you should know whether to pick SqlExpress or one of the SqlCompact options.

Oracle Connection String With Windows Authentication

We have a requirement to make our products work on Oracle as well as SQL Server (around which they were originally built). Unfortunately we don't have any in house Oracle development experience to speak of but as a senior dev it has fallen to me to lead the project. So far I have managed to make our app connect to an Oracle database (I'm using Oracle XE 11.2) by using the following connection string:
Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=VS010-ORA11GR1)(PORT=1523))(CONNECT_DATA=(SERVICE_NAME=DEVORA)));User Id=dbo;Password=dbo;
The reason we decided to use this type of connection string is because we do not want to rely on changing tnsnames.ora on every client machine. However, as you can see this specifies an Oracle user and associated password. We also need to provide the ability to use the equivalent of SQL Server integrated security.
According to the literature I have read, to do this I simply need to specify / as the user id and then omit the password portion (as this is ignored anyway for Windows authentication). I also created the user in Oracle, making sure it matched the Windows user, with the following snippets:
CREATE USER "OPS$<DOMAIN>\<user>" IDENTIFIED EXTERNALLY;
GRANT CONNECT,RESOURCE TO "OPS$<DOMAIN>\<user>";
I also checked that the sqlnet.ora file on my local machine which hosts the XE instance and my dev environment contained the line:
SQLNET.AUTHENTICATION_SERVICES= (NTS)
I understood that this would enable my app to connect to the Oracle database uing Windows Authentication. However what actually happens is that I get the following Oracle error message:
ORA-01005: null password given; logon denied
this doesn't make much sense because of course its null - it's supposed to be, according to the tutorials I've read.
The app targets .Net Framework 3.5, we are using the System.Data.OracleProvider and the actual connecting and so on is handled by Enterprise Library 5. Incidentally, I am aware of the deprecation of the OracleClient component but I just want this to work before I go into the extra complexities of changing providers.
Can anyone tell me what I'm missing? Have I chosen the wrong type of connection string? I apologise for any basic mistakes but I have always managed to avoid Oracle until now so my knowledge of it is close to zero.
Many thanks
I had the same problem and solved after adding this to conn. string:
Integrated Security=yes
To expand on the answer above by #Stikut. I tested this out with NHibernate 3.3.3.GA and it works.
user id=/;password=;Integrated Security=yes

How do I connect to my local SQL instance in Visual Studio?

I've installed VS2010 Express and all associated SQL. I've got an instance running on my local machines of .\sqlexpress and I can connect to it with SSMS. I've created some tables etc.
I want to use Linq to SQL so I have created a new empty .dbml file in my solution. I assume the next step is to connect to the database, but I don't know how as I've never done this before.
I tried Add Connection as it suggests, and the Data Source I can choose Access Database, SQL Server Compact 3.5 or SQL Server Database File.
The Database File one says, "use it to connect to a local instance", so I click that. Then it asks for a database file name, new or existing. Surely I should be typing in the instance name not doing this file thing? If I do type something in, it crease a .mdf in my documents folder and it doesn't really help me much.
Any ideas how I can get this to work?
it's easier than you think.
you create a new database in SQL Management Studio, you call it
whatever, myDatabase...
from Visual Studio you select the SQL Provider and you select the
local instance and the myDatabase as db name.
everything is basically as you said except that you should have created a database and you should then select it in VS.
doing this from a new EF model, VS will store the connection string compatible with EF format in the app.config so you will be able to check it and see how such connection string is written.
I finally figured this out via a blog post or two somewhere online. With the express edition, the option to connect to a local database in the normal way via IP or ./sqlexpress is not present. You have to connect using a File, by finding the .mdf file on your hard disk (within the SQL server folder somewhere) that represents your database, and selecting it.
I have no idea what happens if you update your database schema in SSMS, or why they've restricted it to this bizarre method, but it works and I have been able to query my database.

EF ASP.net app connects to db even after connection strings are deleted in web.config?

Can somebody tell me how EF/asp.net caches db entries? I have a simple mvc3 project hitting a sqlserverexpress db. I want to change the setting in the webconfig to point to a new db but even if I change the connection strings or even delete the section the app still connects to the old database.
EF is a nice thing, but it will do some things without telling you.
You haven't given us much info, but if you're using EF Code First then your connectionstring name must match the name of the DB Context file. So if you have MoviesContext as the DB Context then you should have the same name for the connectionstring.
EF Code First uses a lot of conventions so if it doesn't find a connection string named as the DB Context it will try to connect to a SQLExpress (doesn't care if there is not one installed) and will look for/create the database with a very long name: Namespace.MovieContext.
Note: EF Code First might have gone off and created a database based on your POCO classes, if that is what you have done.
There is no caching involved in this case, it is just that EF Code First created a database for you...

Resources