Why is entity framework giving this error on web server? - c#

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.

Related

Developing with Azure Mobile Services in a local computer[.Net backend]

I'm developing a cloud service using Azure Mobile Services, and in order to test it quickly and debug it, I want to deploy it in my local computer. I select the project, hit F5, got it running in IIS express in my local PC. I execute the cliente against my local URI address of the IIS service and when I try to insert a value, this exception appears if I try to retrieve or insert a new object:
An exception of type 'System.ArgumentException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: The database name 'TR_MyTrip_Server_ExtraData_Service]_Persons_InsertUpdateDelete' is invalid. Database names must be of the form [<schema_name>.]<object_name>.
I debugged the Initialization of the controller and found out that mobile services deploys a LocalDb instance with this connection info on the DataBase property of the ServiceContext object:
Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MyTrip.Server.ExtraData.Service-20140731060708.mdf;Initial Catalog=aspnet-TestProject.Server.ExtraData.Service-20140731060708;Integrated Security=True;MultipleActiveResultSets=True
It is not able to find this DataBase if I try to connect with this connection string to the LocalDB via the SQL Management Studio
I'm able to access to the LocalDb instance with the SQL management studio running this command on CMD and retrieving the Connection for the LocalDB
SqlLocalDB.exe info v11.0
Eitherway, on the SQL Management Studio I'm not able to see any DataBase relating my controller of the Mobile Services. I searched on google and the only related link I found was this but is not working... Does anybody know what's happening?
Thank you so much
This is a problem when you have a project name with periods in it. Like TestProject.Server.ExtraData.Service.
To fix this, go into the web.config and edit the appsetting named "MS_MobileServiceName" taking out the periods. This is the value used as the SQL schema name for your mobile service tables in the database. It can't have periods or other special characters in it.
I generally try to make this the same as the name of the mobile service I'll deploy to, but it's not technically required.
For whatever reason, I couldn't get this working using the accepted answer. I assume this has something to do with the disclaimer in web.config which states:
After publishing to Mobile Services, these settings will be overridden by the values specified in the portal.
However, I did find another work around in the MobileServiceContext file where I replaced this line:
string schema = ServiceSettingsDictionary.GetSchemaName();
if (!string.IsNullOrEmpty(schema))
{
modelBuilder.HasDefaultSchema(schema);
}
with a simple string value modelBuilder.HasDefaultSchema('mySchema');
I imagine there's a way to influence the output of GetSchemaName() but, considering how much effort it took to resolve this problem, I am perfectly content with this solution for now.
If you've changed the MS_MobileServiceName in config to remove the periods, and you're still getting the error, you'll need to re-run the scaffolding for the Initial migration in the package manager console:
Add-Migration Initial -Force
From what I could see, the scaffolding generates an embedded .resx that still has your old schema name (DefaultSchema), and a snapshot (Target) that is referenced in the designer code. Just changing the DefaultSchema didn't solve the issue for me, but re-running the scaffolding did.
I also added Table annotations to my models, but I don't think that was the issue. And probably isn't ideal if your service is already live.

Firebird 1.5 / Asp net VS 2010 (legacy database .CDB extension)

I have an old system which generated me a database in .CDB extension (i run on Firebird-1.5.6.5026-0-Win32) and i can access this database in IBExpert to query and stuff. But i need to write an application in .NET (VS 2010 4.0 framaework) so i can read this database and access some of the data to insert into a table inside SQLServer.
I tried many things, changed the server version and other things but i now all i get is ''Cannot find fbembed.dll'' exception error while trying to open the connection. My FB server doesnt have this file since he uses the 'fbclient.dll' already.
Any thoughts on how to connect my application to this .CDB database?
(this firebird version is the same that the legacy system is running, so i used the 1.7RC firebird .net provider within this server)
The connection string used is:
<add name="FirebirdConnectionString" connectionString="User=SYSDBA;Password=masterkey;
Database=localhost:C:\temp\BD\E‌​CLECTIC.CDB;DataSource=localhost;Port=3051;
Dialect=3;Charset=NONE;Role=;Connectio‌​n lifetime=15; Pooling=false;
MinPoolSize=0; MaxPoolSize=50; Packet Size=8192; ServerType=1;"
providerName="FirebirdSql.Data.FirebirdClient"/>
Unless you really want to use Firebird embedded (which you don't as you also specify localhost), you should not specify ServerType=1, but either leave it out entirely or set ServerType=0.
As to your other problem you mention in the comments, I suggest you check if this solves it and otherwise create a new question with more information.

Part of my project started using incorrect connection string - how do I fix that?

I have one solution with one project in it. This project is an asp.net mvc web application with xsockets.net websocket server (everything merged inside single project).
Everything was working for a few months, until today. Today I decided to update entity framework and xsockets.net. There were few errors on the way, but I solved almost all of them... almost.
Well, the part of my project that runs websocket server is not using correct connection string. I mean, I can login to my web application, and move around it (so asp.net mvc is using correct connection string), but my websocket server (which is using the same database) cannot gather any data from database, since it's throwing incorrect connection string exception.
And since everything is in the single project, with single web.config file, I don't know what to do next. I don't believe that this is websocket related error, maybe entity framework update has changed something? Anyways, is there any way to explicitly use connection string inside of a class? What else I can do to fix that?
When Entity Framework connects to create the entity objects it also establishes the connection string to that database. This usually isn't a problem since that project is referenced by another program that is overriding the server connection string with their own app config (or web config).
Typically in code when connecting to an instance of Sql Server, you write your code:
using ( MyServer context = new MyServer(myconfig.ConnectionStrings["MyServerName"]))
{
}
If you exclude providing he connection string when creating the instance of your context, you risk catching the default value created when you updated entity framework. So this probably answers both your questions: the why is it changed your connection string. The explicit use is the code example above.

Connection string hidden somewhere?

I have two databases on one machine that also has two different sites running on IIS. Lets call them Site1 & Site2 and DB1 & DB2. The second site and BD are copys of the first one. The Site2 connects to DB2 and everything seems to work nice but when we took offline the DB1 it stoped working which is wierd since all the data created using the site2 was in DB2. In fact most of the site works except for one method
We use entity framework to access the database and when we trace the code everything looks ok, but it somehow doesn't work.
In our auto-generated code by entity framework we traced the connectiong string and the outcome is correct
Initial Catalog=DB2;
But in the next line we have this code
return ((IObjectContextAdapter) this).ObjectContext.ExecuteFunction<T_REFERENCE_DATA>(
"GetReferenceData", groupNameParameter);
And we get an inner exception that says:
Database 'DB1' cannot be opened because it is offline.
All the other methods seem to use the correct database except this one. We can't figure out where the DB1 is configured or hardcoded
The problem was (as usual) stupid as hell. Some of the guys defined the DB on the stored procedures. When we changed DB and copied the stored procedures they kept using the old DB. It is now fixed.
Thanks for your help

connect to .mdf file in vs2010 , wpf application

I am using VS2010 , and I built a .mdf file using SQL server 2008
I want to use this database file in my wpf application so that I can add rows to it and delete rows from it
the problem is , I can't access this file , and all the insertion and deleting is actually hapening to the datacontexct i created .
I used myDataContexct.Submitchanges() but it didn't work either
I tried to add a connection string when I define the datacontexct that holds the url of my .mdf file and this it gave me a runtime error when trying to access this file and the error messege says :
An attempt to attach an auto-named database for file Trial.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
please help me because I searched alot but I couldn't find any help
If the application is not going to be installed in a manner that many clients are accessing the same server, you would want to consider using SQL Server Compact Edition.
Are you sure the connection string in your app.config refers to the local mdf file? perhaps it refers to the server instance?
What technology do you use, is it LinqToSql or Entity Framework (I think you have t call SaveChanges, not AcceptChanges)?
If you do intend to access the server instance, then the problem seems to be a security restriction.
Please add more details on statement no. 1, and I'll write further info.

Categories

Resources