Entity Data Model attempting to create database, rather than use existing - c#

I cannot figure out why I'm receiving the above error in my ASP.NET MVC site.
Some background, this site has existed and has been working in my DEV environment for several months. I'm using SQL Server and the ADO.NET Entity Data Model to `Update the Model from the Database'. Recently, I did some pretty large updates that involved changing a few tables around. Then I began receiving this error.
What I've tried:
Clearing browser cache, and using different browsers
deleting all generated tables in the .EDMX file and re-updating them
deleteing the .EDMX file entirely and recreating it
confirming my connection strings (the web.config hasn't been updated at all)
the server is working fine. I can use it in all other apps and SSMS
begging to no one in particular
below is the connection string created:
<add name="RetailDB" connectionString="metadata=res://*/Helpers.RetailDB.csdl|res://*/Helpers.RetailDB.ssdl|res://*/Helpers.RetailDB.msl;provider=System.Data.SqlClient;provider connection string="data source=****;initial catalog=RetailDatabase;persist security info=True;user id=******;password=******;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
I'm sure this is probably something I'm overlooking, but I don't understand why it has decided to start trying to create the database itself. Please let me know what other information I can provide.

Related

How to change connexion string in a deployed Azure App

I have developed a website application under visual studio using the MVC Pattern and Entity Framework 6.
This web app was linked to a local database.
I've published the application on my azure acc. I also exported the database on Azure following this tutorial.
However, I can't find a way to link the online app to the migrated database.
No data is displayed on the app and when I try to check for the connexion string I get an error , the connexion string hasn't been updated and therefore the app is still trying to target my local database.
I would like to know if anyone might know how to solve this.
EDIT : The problem may be somewhere else, I apparently have two connexions , one for my migrated DB and one for my local db. Perhaps I need to remove the connexion string targetting my local DB but I don't know how to do that
Also, since the Migrated database is a copy from the local one I was using before, do I need to change anything in my Model,views or controller ?
EDIT2 : Thanks to the comment below I was able to override the false Connexion String. However my datas are still not displayed on my application and I haven't found yet what could have happend.
I suspect that my model is still the same model as the one used by the local database as it has the same name. Does anybody know how to change the model to the current model of the online database ?
Thanks.
Because you created your app with entity framework there should be a connection string defined in the web.config file pointing to the local database.
<connectionStrings>
<add name="MyContext" connectionString="..."
providerName="System.Data.SqlClient" />
</connectionStrings>
If you have published your Website as WebApp (App Service) then you can change the connection string for the database under the Application Settings pane on the WebApp Page.
You can override your connection string by creating a connection string settings entry with the name of the connection string in the web.config file.
(For the snippet above - MyContext)

Do you have to publish DefaultConnection containing the membership tables?

Attempting to publish a website to my host which contains my custom database and the default like so:
<add name="PortfolioContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Portfolio;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
When publishing the site I can't login which I assume is down to because the DefaultConnection db isn't being created (containing the membership user tables), I might be wrong? I can generate the script for my custom database in Management studio but DefaultConnection doesn't even appear to allow me to manually create tables associated.
My error log displays this
2016-02-02 12:28:35 W3SVC30 217.194.213.135 GET /Login
ReturnUrl=%2Fdashboard 80 - 95.147.124.217
Mozilla/5.0+(Windows+NT+10.0;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/48.0.2564.97+Safari/537.36
- 500 0 0 1198 416 203
Though I'm not sure what that means. Sorry this is my first attempt at publishing a site. Thanks
Edit:
Could not find a part of the path 'C:\HostingSpaces\hannahha\hannahhamlin.co.uk\wwwroot\Assets\Common\images'.
This is the error it displays. This leads to me a problem I knew i was going to have to confront anyway. How do I include necessary directories for publishing?
First, that's not an error log. That's IIS' default request log. All it tells you is that a client using a particular browser requested /Login via GET. In other words, nothing at all useful to this issue.
You need a real error, with a stacktrace. You can either turn off custom errors temporarily (<customErrors mode="Off" /> in your Web.config), install something like Elmah to log errors for you, or use remote debugging.
However, most likely, the connection string only works locally, and the error you would see is just a DB connection error. It's rare that the connection string you'd be using in development would match exactly with what would be required for production, so you should utilize config transforms to change it when publishing.
That's assuming of course you even have a database server set up in production to connect to. Visual Studio employs a lot of database magic to allow you to focus on your code rather than menial implementation details like connecting to a database. However, when you go live, the training wheels are off, and you're responsible for making sure everything necessary is wired up. If you don't have a database server, install one. If you don't have a database, create one. Then, just alter your connection string to connect to that.
UPDATE
The error message is so far removed from this question was originally about, I think you would be best served by creating a new question and deleting this one.
That said, I'm not sure what you're trying to achieve exactly, but anything under your project directory will be published along with your site (except certain special directories like App_Data). If the files you're referencing are in some directory outside of your project directory, they will not be included, and there's really not a way to include them. You can, however, create a build action that copies the files somewhere under your project, and by virtue of that, they should then be published.

Why is entity framework giving this error on web server?

I'm trying to use Entity Framework to add records to a database from a webform input that go into a database on sqlserver.
Everything works fine locally.
I used webmatrix to publish my site to my remote server, the website and my local version of the database is successfully recreated on the remote server with all the data.
However when it comes time to add records to the database, it gives an error on the remote server which i managed to trace to the ctx.Students.Add line.
using (var ctx = new HDPS_SchoolDataEntities())
{
SchoolDataModel.Student temp = new SchoolDataModel.Student();
temp.Name = this.FirstName;
temp.Surname = this.Surname;
temp.Parents = this.Parents;
ctx.Students.Add(temp);
ctx.SaveChanges();
}
I can't find any difference between my local version that works and the one on the remote server that doesn't work. The web.config seems to be configured as expected and all the necessary dll's are in my bin folder but it just won't work on the remote server... any ideas what could be wrong?
Oops forgot the error message!
edit
after Installing .net 4.5 and changing the connection mode from windows authentication to SQL authentication the error now becomes:
Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.
Everything should be the same on the webserver as the entire website is being copied across but it works fine locally so not sure what the problem is...
OKay I solved the issue with the help of this webpage
http://blog.oneunicorn.com/2012/02/26/dont-use-code-first-by-mistake/
In short It seems webmatrix was does not include the metadata part of the connection string on upload to the server thus the EDMX file containing all the information mapping the classes to the database tables was not being referenced causing any queries to the framework to fail.
Once i pasted the original connection string generated by the Entity Framework wizard onto the server everything worked fine.
Based on the error given in your comment, it sounds like you are failing to connect to the database. Check your connection string, username and password. If those are correct, make sure your database settings allows connections from your server ip address.
OKay I solved the issue with the help of this webpage
http://blog.oneunicorn.com/2012/02/26/dont-use-code-first-by-mistake/
It seems webmatrix was does not include the metadata part of the connection string on upload to the server thus the EDMX file containing all the information mapping the classes to the database tables was not being referenced causing any queries to the framework to fail.
Once i pasted the original connection string generated by the Entity Framework wizard onto the server everything worked fine.

Can't connect to shared hosting database with site

Slightly at my wits end. Built an application, running fine locally. Migrated my database to MSSQL without issue, uploaded the site, can't seem to get the application to connect to the database. Any page that accesses the database I get a generic error message.
I've tried all the separate combinations of connection strings I could think of using the Godaddy recommended connection strings. Perhaps I am overlooking something simple?
I'm using Entity Framework Code-First -- My context model is called CombosContext.
<add name="CombosContext" connectionString=" Server=jelatin.db.9508732.hostedresource.com; Database=jelatin; User ID=jelatin; Password=********; Trusted_Connection=False" providerName="System.Data.SqlClient" />
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString=" Server=jelatin.db.9508732.hostedresource.com; Database=jelatin; User ID=jelatin; Password=********; Trusted_Connection=False" providerName="System.Data.SqlClient" />
Server: jelatin.db.9508732.hostedresource.com
DB name: jelatin
user: jelatin
Table: Comboes
I'm unfamiliar with GoDaddy hosted SQL, but usually a connection string to MS SQL Server uses "Data Source" instead of "Server" and "Initial Catalog" instead of "Database".
UPDATE
I didn't realize Server and Database were allowed options in the Connection String. Sorry for the confusion.
Regarding the database itself - are you letting EF create the database? Does the user have permission on GoDaddy's system to create a database?
If you have already created the database, did you populate anything? I have found that EF Code First won't correctly populate the database if the database exists and the metadata table doesn't. If you can, try copying your local database up to GoDaddy, and see if the connection works.
Finally, for your generic error message - is it coming back in a 500 error? If so, have you tried using either IE or Chrome's dev tools to inspect the response? Better error information is usually hidden in there.

Uploading ASP.NET 3.5 website

I have a website writen in ASP.NET and C#.
The site works when run locally, however I don't have any experience with uploading. Recently I got a free hosting package from somee.com and am currently trying to upload it.
Basically, the pages that don't require database connection work, the ones that do don't. So, I've created a database on somee.com and attached my database to that. I've tested it through somee.com control panel with a couple of querys and it works fine.
So I believe the site is uploaded correctly, database works also. I figure it must be a connection problem. In my codebehind, every database connection uses the web.config connection string. I believe there is a problem somewhere in the web.config file, specifically with the connection string.
I have replaced my connection string with the connection string generated by the somee.com control panel which is:
<add name="feniks_dbConnectionString"
connectionString="workstation id=SQLEXPRESS003.mssql.somee.com;packet size=4096;user id=*;pwd=*;data source=SQLEXPRESS003.mssql.somee.com;persist security info=False;initial catalog=SQLEXPRESS003"
providerName="System.Data.SqlClient" />
I've marked my id and pass with *. I keep getting the same error, you can try it yourself on http://aspsitetest.somee.com/index.aspx.
Error on page not about sql-connect - it writes about wrong web.config
search for duplicate:
There is a duplicate 'system.web.extensions/scripting/scriptResourceHandler' section defined
If the same web.config works on local machine, maybe you use another version of IIS.

Categories

Resources