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
Related
I have (I think) a very strange problem.
I have a web server running an ASP.NET MVC app querying a SQL Server database. It has all been constructed using code-first approach.
It has been working without problems for a long time, and whenever I have needed e.g. a new column in a table, I have added the needed class to the model in question. Then I have run Add-migration 'Migration_Name', and a script has been generating with the proposed changes, and after running update-database, the changes have been applied to the database. Easy and always working.
Yesterday, without making any changes to the database or the models (and by the way not even updating the web-server) it stopped working.
The database throws an error the moment the web-app tries to access the database
System.Data.SqlClient.SqlException: 'CREATE TABLE permission denied in database 'master'
The content of the database is intact I can see from browsing it from within Visual Studio.
If I run the add-migration command, a script is generated for every single table, column, foreign key etc for the entire database, as if the database did not contain a single one of the classes defined in the models.
So it seems that the database is not in sync with my code first models anymore. I got my IT department to copy the entire database to a test server to try to test what is wrong without messing up the production database, and after trying to run the "strange" migration script by using the update-database command, I get the same error:
CREATE TABLE permission denied in database 'master'
and it shows me that it fails when trying to create the first table in the migration script. This is not surprising, since it already exists in the database. But what is going on - I am at a loss!
So can anyone tell me might have happened here, and more importantly, how to fix it?
All the best
Troels
I had some problems with using the authorization before so I got a brand new everything - new computer, new OS, fresh installation of VS, new app and DB in a new resource group on the Azure. The whole shabang.
I can confirm that I can log in to the Azure DB as the screenshots below show.
I can see the databases, tables, users etc.
The problem is that, although it works locally (using the default connection string provided automagically for me), it doesn't perform very well in the Azure (although I'm using the publish file from there). It said something about the file not being found and according to this answer, I needed to change the connection string.
After I've altered it, I get the following error. Please note that the firewall is open and that I can access the DB when I run the code of my applications. I feel that there's something that goes wrong when the authentication part is automatically configured. I'm out of ideas on how to trouble-shoot it, though.
[SqlException (0x80131904): Login failed for user 'Chamster'.
This session has been assigned a tracing ID of '09121235-87f3-4a92-a371-50bc475306ca'. Provide this tracing ID to customer support when you need assistance.]
The connection string I'm using is this.
Server=tcp:f8goq0bvq7.database.windows.net,1433;
Database=Squicker;
User ID=Chamster#f8goq0bvq7;
Password=Abc123();
Encrypt=True;
TrustServerCertificate=False;
Connection Timeout=10;
This issue's bothered me for a while and I'll be bounting it in two days. Any suggestion's warmly appreciated.
I believe I've managed to resolve this weird issue. It appears that the user I'm using, despite being admin with all bells and whistles isn't recognized as admin when used in the connection string and trying to create the tables (which is the case at the first registration).
My solution was to create two logins - one with db_owner role and one with db_datareader and db_datawriter. First, I've used the elevated user in my connection string and registered a single user. That created the tables in the database as shown below.
Then, while able to continue as admin, I realized that we should try the demoted user and tada!, it worked perfectly. Once the tables were there, the whole shabeling behaved as expected.
To be perfectly sure, I dropped the tables from the database and there it was - the same issues as before. When I changed to the elevated user, the tables were restored allowing me to get back to the demoted one.
I also tried dropping the tables, confirming the issues to re-appear and then creating the tables manually. That works too! So basically,the only gotcha that caused it all was the original admin who's not treated as admin.
It might have to do with the fact that my Azure account's getting a bit old, LiveID used there is ancient and that didn't have an updated version of DB in Azure (the pull-up to v12 was carried out the 18th of December, so it's possible that it also was a requirement to get it working). I'm too tired and lazy to check that out and I realize that I've no idea how to get an "old" type of account. Besides, the issue will decrease and gradually vanish because the old accounts get upgraded eventually.
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.
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.
I am trying to create a database, but once created, I cannot connect to it.
The server is Microsoft SQL Server 2008 and using .Net 4.5. We're creating the database with SMO, but we're usually using Dapper to connect and query the database.
This is the code I have so far, which works :
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
Microsoft.SqlServer.Management.Smo.Server srv = new Microsoft.SqlServer.Management.Smo.Server(new Microsoft.SqlServer.Management.Common.ServerConnection(con));
var database = new Microsoft.SqlServer.Management.Smo.Database(srv, dbName);
database.Create(false);
database.Roles["db_datareader"].AddMember(???);
database.Roles["db_datawriter"].AddMember(???);
database.Roles["db_backupoperator"].AddMember(???);
srv.Refresh();
Noce the ??? ? I have tried
System.Environment.UserDomainName + "\\" + System.Environment.UserName
and
System.Environment.UserName
but it fails (update) with the error Add member failed for DatabaseRole 'db_datareader'. with both values.
The problem is that when I create the database, I cannot coonect to it for some reason (using Dapper), from the same program. (update) I get the error message : Cannot open database \"<database_name>\" requested by the login. The login failed.\r\nLogin failed for user '<domain>\\<username>' (where <database_name> is the database name, <domain> my logon domain, and <username> my Windows logon).
Am I missing something? Am I doing th right thing? I've tried searching the web, but it seems no one creates database this way. The methods are there, it should work, no?
** Update **
If I comment the database.Roles["..."].AddMember(...) lines, and I add a break point at srv.Refresh(), resuming the program from there solves everything.
Why a break point solves everything? I can't just break the program in production... nor break the program when creating the database everytime.
It sounds like the Dapper connection issue is a problem with SQL Server doing some of the SMO operations asynchronously. In all likelihood, the new Database is not ready for other users/connections immediately, but requires some small time for SQL Server to prepare it. In "human-time" (in SSMS, or a Breakpoint) this isn't noticeable, but "program-time" it too fast, so you probably need to give it a pause.
This may also be the problem with the Role's AddMember, but there a a number of things that could be wrong here, and we do not have enough information to tell. (specifically, does AddMember work later on? and are the strings being passed correct or not?)
This is happening because you've created the user, but no login for that user. Though I don't know the exact syntax, you're going to have to create a Login. You'll want to set its LoginType to LoginType.WindowsUser. Further, you'll likely need to set the WindowsLoginAccessType to WindowsLoginAccessType.Grant and you'll need to set the Credential by building one, probably a NetworkCredential with the user name you want.
To put a visual on this, the Login is under the Security node for the Server in Management Studio whereas the User is under the Security node for the Database. Both need to exist for access to the SQL Server.