Entity Framework error connecting to SQL Server Express after mounting/connecting in Visual Studio - sql-server-express

Just in case I am doing something wrong, I have uploaded a video so you can see every step I have done! I am able to reproduce this error.
Video Link
Basically - Start a new MVC 3 project and using EF in the normal way on a simple class.
I used the following connection string:
<add name="databaseconnection" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|database.mdf;User Instance=true;Initial Catalog=database" providerName="System.Data.SqlClient" />
When I navigate to the site, Everything works ok and as expected.
Now, if I try to double click the .mdf database to open it inside of Visual Studio, all hell breaks loose!
I can connect and view the tables as expected, but even if I do nothing and just close (and/or) delete the connection, next time I go to the application, I get the following error:
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.
Cannot open database "database"
requested by the login. The login
failed. Login failed for user
'WIL2-8EEA651803\Administrator'. Log
file 'c:\documents and
settings\administrator\my
documents\visual studio
2010\Projects\TestingEF\TestingEF\App_Data\database.ldf'
does not match the primary file. It
may be from a different database or
the log may have been rebuilt
previously.
No matter what I try, I can't reconnect - the only thing I can do is to rename the database and initial catalog. I am sure that the previous version of the MVC Movie Database Tutorial (before they updated to using compact framework) worked fine like this, so I am unsure why I am having these issues.
So, my question is, what is causing this and what should I be doing instead?
Next, Error number 1 in the video, this is a quick XP VM I use for testing, and it happens at random, but did not happen this time and has never happened on the main machine - so I am curious, but not that bothered. When attempting to build the Controller, I get the following error:
"Unable to retrieve metadata for 'TestingEF.Models.blabla'. The provider did not return a ProviderManifestToken string."
Any idea what this is?

I know for the second issue I had to manully drop the database to get this to go away. I think it has to do with the model and the metadata for the model not being in sync int he database.

Related

On Database Change: Code First mode with code generated from an EDMX file for either Database First

I have two environments on different servers, development and test.
When I deploy my MVC site to both servers provided there both pointing to the development database everything works fine, As soon as i change the connection string to use the test servers database I receive the following error..
"The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development."
What would cause this issue? from my viewpoint the database is present on both servers and their tables etc are the same.
Solution: Turned out that the connection string name had been changed, reverting this change has allowed the application to load without error.

How to fix "login failed for user" when manipulating SQL database in Azure from a .NET Core webapp?

I have an SQL Database in Azure and a .Net Core web app in which I would like to manipulate the database using Entity Framework Core.
I believe I have all the model, data and controller classes needed for this, and I have a few sample objects I'd like to see in my database.
My problem is that no matter what I do or how I want to manipulate the database, I always get the error "login failed for user", yet the login info is an admin and I believe I set up everything that is needed.
I see the database in the SQL Server Object Explorer, I can connect to it using SSMS or simply on the Azure Portal, and there seems to be no problem with my connection string. The username&password combo is fine, I've checked it multiple times, my IP is enabled in the firewall of the database, I have no idea what could be the problem, please help me!
Here is the error:
The problem is solved now, it was a very tiny, stupid error:
the connection string I copied from the Azure site was waiting for an escaped field, assuming I won't write my username and password manually, which of course won't be happening in the future, I just wanted to test if the whole stuff worked as this is my first project in this area.
So the Azure connection string looked like this: USER={XY};PASSWORD={ASD} but I left the curly brackets in the string even though I didn't use a variable there.
I test and it works fine. So I am doubt about whether you could run it in local with the connectionstring of Azure sql database.
Open your azure sql server on portal and click your sql database.
Click the Connection string and copy the connectionstring. Filled into your username and password.
Then put it to the connection string in appsettings.json on visual studio.
If you could run on local, then you publish will have no problem.
Also, you could follow this article to learn with asp.net core mvc.

Local Database: Not loading all the changes of the tables and views

I am using Visual Studio 2012 and an SQL Server Database.
At first, my program loads all the tables as well as views but when I try to do some modifications to the database schema (e.g. add some views) the changes are not reflected when I deleted the connection in the Solution Explorer-Model and then reconnect it.
I tried to delete the connection again, load the database and reconnect, but still, I get same results.
Why is this so? Any idea?
First, I make a database by clicking App_Data and add new Item. I add SQL Server Database. So that's why the entities has a .mdf extension.
Next, I add new tables on the database by right clicking the "Tables" and Add new Table on it. Insert new columns as well as the keys.
Then, I connect to the database by Right clicking Model in the Solution Explorer and add ADO.Net Entity Model. I used the Database.mdf for my connection. After that, it automatically generates the connection string in the web.config as well as the diagram.
Usually, when I made some changes in the database, I deleted the connection and reconnect again. Before I had no problems with it. The changes were seen upon reconnection but this time when I tried using with another app, I'm wondering why the modifications were not seen. I tried many times to connect and delete again the database but still nothing works.
As I understand you, you works directly with .mdf file. Consecuently, each time when you starts your application it copies into Debug folder and if you work with this local copy (it depends on your connection string in App.config) you change data exactly in Debug folder. So, each time you erase your changes when application starts, try to changes your connnection string.

Connection Error with SQL Azure and Entity Framework on Azure Website

Not sure where to start, but whenever I publish my ASP.NET website to Azure, any pages which have database access give me a message saying "Error. An Error occurred while processing your request." I open up the remote debugger (which is fickle because it refuses to attach half of the time) and I see the error occurs when establishing when trying to access Entity Framework. The error varies between a "network-related or instance-specific" error, or a "Login Failed" error (which could be the result of the previous error, I really don't know).
The ADO.NET connection string SQL Azure gives is
Server=tcp:[servername].database.windows.net,1433;Database=EnsembleMusicWebDatabase;User ID=user#[servername];Password=(password);Trusted_Connection=False;Connection Timeout=30;
But every implementation (inserting that into EF metadata string, changing the server to data source...etc) still gives me the same login error
I'm pretty sure it's a problem with the connection string, but the infuriating part is that I've tried every possible combination I can think of (entity framework metadata, using the SQL Azure database ADO.NET connection strings in any possible way, changing the Azure website connection strings under the Config tab, using just a plain connection string...etc).
I've deleted and rebuilt the Entity Framework models at least 5 times, and every time I can successfully establish a connection to the server and it successfully reads my database and creates the correct models. I deploy the application to localhost and it works. The problem is when I publish, it cannot access the database and keeps giving me these login failed errors (the login details are the exact same as when I set up the EF model).
I think it might be something to do with the firewall, since I can access the DB locally with an approved firewall IP on the server config, but the website itself can't access the database (I have the enable azure services box ticked as well). I'm really at a loss for what to do now, because I just want the site (not any user, just the application) to fetch some data from the database and display it on the page, but I don't understand how this could be so complicated.
Can anyone point me in the right direction? I tried every tutorial and example on msdn and I can't find any solutions on SO that work.
Thanks,
Shaun
I realised that I somehow got into a complete mess with connection strings all over the place and the best way to fix it was just to start again. I deleted my Azure website and database instances, built the database first (created a correct login as well) and then when creating a new website Azure gave me the option to include the database I had just created. I now have a correct connection string that Azure generated, but to be safe (because the metadata made connection strings confusing and I didn't want to risk having this same issue again) I'm not using Entity Framework and just using normal SqlClient queries, since the website only requests two objects from a database.
I think now I've got a correct and working string I can look at it and really understand where I went wrong and how to avoid this if I do end up using Entity Framework.

Deleting database causes exception until I rename catalog in Web.config

If I delete my aspet-*.mdf file and then try re-running my project I get this exception
An exception occurred while initializing the database. See the InnerException for details.
the first time it tries to hit the DB. If I then go into my main Web.config file and change the "Initial Catalog" bit to a different name and re-run the project, it runs fine.
Why? I'm guessing it's caching something somewhere and it thinks that catalog still exists, but it can't find it?
But I don't have to change the AttachDBFilename -- that filename can be exactly the same and it will recreate it as long as I change the catalog.
What is exactly is a "catalog" and how do I fix this problem so that I don't have to keep modifying my connection string?
The full string I'm using is
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Sharpix-20120918b;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Sharpix-20120907.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
Which is only slightly modified from the one that came with MVC4 application.
Assuming your connection string is similar to below
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-123123;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-123123.mdf"
1) Using SQL management studio log on to the data source
Server Type: Database Engine
Server Name: (LocalDb)\v11.0
Authentication: Windows Authentication
2) Once this opens up you will see all the databases that seemingly are being mysteriously created.
3) Right click on the database you want deleted and follow the dialog windows.
The localDB engine, new vof VS 2012, stores all of these development databases. In case you physically delete the .mdf, right click on the database name and delete the database anyway. Ignore the warning message, and then refresh the Databases. The database reference should be gone.
You can do something similar through VS 2012 by selecting View, Server Explorer and then selecting the SQL Server Object Explorer selection from the Server Explorer pane.