LocalDb Unspecified Initial Catalog? - asp.net-mvc

I've recently been placed on a project using EF 6.0 and Code First principles. The ApplicationDbContext is loaded from this connection string:
name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Integrated Security=True" providerName="System.Data.SqlClient"
As you can see there is no specified Initial Catalog, and yet the data is being stored SOMEWHERE? I have searched the LocalDb instance through the file system, MS VS 2013 and SQL Server Management studio.
The problem is the current devs have created a substantial amount of dummy data we don't want to lose. Does anyone know where this data is being stored and/or how it can be retrieved?
Many thanks.

When a user opens a connection without specifying the DB (aka catalog), it gets a connection to his default DB.
If you have not changed the configuration it will most probably be the master DB.
You can confimr this using SSMS. Connect to your server, go to Security > Logins, choose the login used to connect to the DB (as you're using Integrated security it must be the user which starts the app, or the application pool user if it's a web app), right click, choose "Properties", and, in the General panel, at the bottom, you'll see what the default DB is.
NOTE: perhaps I'm not using the exact wording, as I have a localized SSMS, and I am supposing what the translation is.

Related

First time run configuration (ASP MVC 4)

I'm creating a simple MVC CMS for which I need a first time run configuration (to set up the database and admin user account, etc.).
The setup screen will ask them for the database connection string, so at first run, there is no knowledge of a database store.
How would I detect that this is the first time the application is being run, and take them to that setup screen?
Should I put a setting in the web.config with an initial value of false:
<add key="SetupComplete" value="false" />
And once the setup is complete, I can change it via:
ConfigurationManager.AppSettings.Set("SetupComplete", "True");
The downfall of this method is that, if the application is restarted, the config value will default to "false". What is a good solution to this problem?
Many PHP CMS doing the same thing you want to do, as it is the initial setup it would be OK that the admin change this value manually or you simply check the existence of a file which must be deleted manually.
As it is IIS app I would build a deployment package, so you can setup the the initial settings during the installation process, which is the common way for IIS apps I think.
In the past, I implemented a custom SettingsProvider (an example is here and here). Special care was taken regarding security, ensuring keys are encrypted. The samples isn't perfect. You can use the Application Settings provided by Visual Studio and .NET 2.0 to store settings in the format (e.g. xml) and location of your choosing (e.g. AppData).
It is a tad more secure than allowing your application to modify your web.config, since doing the latter means you have to elevate the privileges of your process which may be rather dangerous when your application is exposed on the internet.
It also simplify backups and upgrades.

Entity Framework creating settings table that I can't put on Azure

I've been trying to create an ASP.net - MVC app that uses entity framework to connect to an Azure database.
I used a database first method to try and create the Model, I was able to connect to my azure database and successfully create the model.
Everything was working perfectly and I was able to retrieve my values from my azure database then I tried to publish the website to an Azure website. The website works fine and I have the website registered as able to use the azure database. When I click on the button that hits the controller to access the database I then get "Sorry, an error occurred while processing your request."
When I looked into this a bit more I seen that when I created my Entity Data model using the wizard it created a connection string to a local db for my entity connection settings. I'm guessing this is saving some entity framework settings but my problem is obviously when I publish the website to Azure it can't access this local db.
Any suggestions on how to either get this DB onto Azure or avoid using this extra database?
Thanks
This is the best tool to migrate your local database to Azure. Before publishing your site change the web.config to point to the Azure database. It will solve your problem.
Like you already explained, it seems that you web.config still points to your local database. Try to look at your web.config and see if you have connection strings pointing to .\SQLExpress, localhost\SQLExpress, ".", ...
Then, like Geenthanga's explains, you need to modify this value before deploying (it should point to your SQL Azure database instead). But you shouldn't do this manually! Instead, consider using web.config transformations, as explained here: Web Deployment: Web.Config Transformation.
This way you can have 2 web.config transformation files:
Web.Debug.config: This file would contain the transformations to add the connection string pointing to your local database.
Web.Release.config: This file would contain the transformations to add the connection string pointing to SQL Azure.

Do I need to have a user name and password on a dedicated IIS server to connect to SQL Server Express?

I use the following connection string when working on my dev machine:
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
This works every time.
However, when I go to deploy to a remote IIS server that is a dedicated host (SQLEXPRESS should work just fine without the worries associated with a shared server), the same connection string results in a failure to connect to the database.
The database files are stored in App_Data within the directory of the application on the development environment and on the deployment environment. I just can't seem to connect to the database on the deployment environment (although the file is sitting there).
Do I need to have my host somehow give me a username / password so I can add it to the connection string? Or do I need to change my connection string in some other way?
I've never used SQLExpress (well not in anger) but remember that once you're on IIS you're running under a different account than when you're debugging. This would be the account under which the IIS app pool is configured to run. So, when you say you want to use integrated security to access this database, you need to configure it to recognize that particular account (whichever it is, for example NETWORK_SERVICE).
In a hosting environment you're probably better off just using SQL authentication, although that's obviously less secure since you have to store the password somewhere. And you'd still have to change the DB security settings.
Otherwise, if you do know the account under which the app pool is running then it's just a matter of configuring the DB to accept connections from it.

Database error in ASP.NET MVC3 EF4.1 code first

Hi i have a working app that uses an attached mdf file with the following connect string :
<add name="SqlDataContext" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=true;User Instance=true;AttachDBFilename=|DataDirectory|\WPCloudApp26.mdf;Initial Catalog=WPCloudApp26;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
I am using a Windows phone cloud application template and i have azure storage emulator running on my pc
everything works fine, until i do the following steps for the first time:
i want to view the database so i double click the app_data/WPCloudApp26.mdf file through visual studio when the app is not running. when i double click it, the db opens up under data connections in the server explorer pane and i can see the database and all the tables. so far everything good
I am done viewing the db, so i close the connection by right clicking --> close connection.
i want to run the app again, and when i run it all hell breaks loose. i get the following error message:
One or more files do not match the primary file of the database. If
you are attempting to attach a database, retry the operation with the
correct files. If this is an existing database, the file may be
corrupted and should be restored from a backup.\r\nCannot open
database \"WPCloudApp26\" requested by the login. The login
failed.\r\nLogin failed for user 'NT AUTHORITY\NETWORK
SERVICE'.\r\nLog file 'C:\Users\michael\Documents\Visual Studio
2010\Projects\WPCloudApp26\WPCloudApp26\WPCloudApp26.Web\App_Data\WPCloudApp26.ldf'
does not match the primary file. It may be from a different database
or the log may have been rebuilt previously.
the exception is thrown on the method var firstItem = context.SqlSampleData.FirstOrDefault();
Thats it, no matter what i do, i can't run the app again. this is driving me crazy since the only thing to help is to create a new solution with a different name (thats why i got to number 26)
What is going on ? why is this happening only after the first time i try to view the database in visual studio ?
Edit
i have noticed that changing the initial catalog and mdf file in the connection string solves the problem. So where are the remnants of the db name are saved ? how can i clean up old leftovers ?
You shouldn't try to open a .MDF file that is attached to SQL Server Express. It will probably break the consistency between the .MDF and the log file.
Connect to the SQL Server Express server using the Server Explorer, or SQL Server Management Studio. (There's a free version of it for Express as well)
If you really want to open the .MDF in Visual Studio, then detach the DB from SQL Server Express first.

Attempting to build and run Shrinkr using Entity Framework

Recently at work, I was tasked with implementing a URL shortener for my department to use internally. We're largely a ASP.NET MVC shop (whenever possible), so naturally I gravitated towards the open source http://shrinkr.codeplex.com/. However, Entity Framework is foreign to me and I have no idea how to actually get this up and running. I've created a SQL08R2 database for it, and passed it (what should be) a valid connection string:
<add name="Shrinkr" connectionString="Data Source=sql08r2.myFQDN;Initial Catalog=Shrinkr;Integrated Security=True" providerName="System.Data.SqlClient" />
Building and running it in Debug mode yields the following error:
Of course, my database is empty and has no table structure. I just can't figure out how the heck to get the database populated with the correct initial schema. I've heard that EF can autogenerate the database, but I'm not sure if that applies in this case. If there is a SQL file in the Shrinkr release to get the DB up and running, I sure as heck can't find it. I've been banging my head against my desk for a few days now on this and I can't figure out what I'm missing!
I just downloaded the source, and there is a create.sql script in the Database folder. Sounds like that is what you're looking for.

Resources