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

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.

Related

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.

Reporting Services multiple report folders, same data source name, but want different DB

TThanks, for reading, I'll try to explain my issue in a detailed format as the question I'm asking is a bit high-level for my experience-level.
I'm using VS2005 and SQL Server 2005 with Reporting Services. All of my reports are built in VS2005. The reports are deployed to folders named "Amort" or :Amort_Test" on the Report server depending on the configuration I choose when I deply (Production delpoys to "Amort", Test deploys to "Amort_Test").
In Reporting Services Report manager, I have a data source setup call AMORT (and that is the datasource in my VS2005 reports). The datasource is of type Microsfot SQL Server and the connection string is "Data Source=uslibsql310;Initial Catalog=AMORT_P".
What I'd like to do is have the ability for the reports in the "Amort" folder point to a database called AMORT_P on my server (uslibsql310) while the reports in the folder "Amort_Test" point to the database called AMORT_T on the same server (uslibsql310). Obviously my current configuration, where reports in both folders point to the same datasource, says that reports point to the AMORT datasource which currently points to AMORT_P.
My initial thought was that I could create a new datasources, call it AMORT_Test and have its connection string be ""Data Source=uslibsql310;Initial Catalog=AMORT_P". However, every time I'd deploy my reports, I'd have to change the datasource in VS2005 to read AMORT_Test instead of AMort and then deply, which would be abit of a hassle.
Can anyone think of a more user-friendly solution to this? I'm one who normally finds the quickest solution and goes with it, but in this case I think there must be a way to set this up so that the reports in one folder know to pick one DB and the reports in another folder know to pick a different DB, but my current setup doesn't allow that. I'm not sure where to start in trying to figure this out as I'm a bit of an RS novice.
You're almost there, I think. If I understood correctly, here's your current setup:
One shared datasource
Reports all use that shared datasource for datasets
Two configurations: test and production, each with its own target folder
What you can do now is set OverwriteDataSources to False. Manual labor is required to set the connection string for deployed reports only:
For initial deployment of reports
When you want/choose to change the connection for deployed reports
This manual labor can be either:
Changing the connection string, temporarily enable OverwriteDataSources, and re-deploy
Going to the report manager web frontend to change the connection string
However, your default setup would be to deploy reports to both configurations, without having to worry about connecting test reports to production databases and vice versa.

How to deploy windows form application with entity framework

I am busy developing my own application, it uses a sql server database and it is connected through an entity framework. I use store procedures to insert, update, delete, select from my database.
The app works perfect on my machine even when I publish it. But my problem comes in when I try to install the app to my friend’s computer. It crashes and does not start because it cannot connect to the database.
Is there a way to publish my app with the database, without importing all the tables, store procedures and database into my friend’s pc? I just want to make it so the user just has to install the app and it works.
Your app relies on the db to work, so if you want to put it on your friends PC then you need to make the DB available somewhere, whether it be a local copy or a copy stored on a server somewhere running SQL.
How are you storing your connection string? Is it hard coded in the app or are you utilising the app.config file? To do what you're trying to do you'll need to put the connection string into the app.config file, so you can change it depending on the installation.
either that or
if you want to run your app without data, put a demo flag or something into the app.config file. Put some code into your app to check this value, if it's true then bypass the SQL code and maybe supply some demo data which is hard coded.
Does this make sense?
You could use SQL CE, but you may find it a little more difficult to 'design' your database in it if you are more used to working in SQL Server.
Have you considered SQL Server Express as an option?
On the connection string issue, you can now get the data connection wizard that Microsoft use in Visual Studio via Nuget; this makes adding a way to dynamically configure connection strings on your clients machine much easier.
Lastly, connection strings for the entity framework are different from standard SQL connection strings. Make sure that you clearly understand the differences before you start trying to configure them programmatically. Julie Lerman's excellent book on the Entity Framework explains the differences well.

Entity Framework error connecting to SQL Server Express after mounting/connecting in Visual Studio

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.