DB First, MVC 4, CREATE DATABASE permission denied in database 'master' - entity-framework

I keep getting this error on my server when I publish my site - even without changes in database model.
The site is running fine local.
I'm using the DB first approach.
If I delete all files in the site on the server, restore my local database on the server. Publish the site from VS. Sometime it will run fine after that.
If I then make a few changes in the database CONTENT I will get the error.
I don't whant to create any database/tables on the server - just disable the part of the code, that need to do it. In MVC3 we had this DropCreateDatabaseIfModelChanges but I can't see any of that in my current site.
What can I do?
My connectionstring:
<connectionStrings>
<add name="GoBagHomeComEntities" connectionString="metadata=res://*/Models.GoBagHomeModel.csdl|res://*/Models.GoBagHomeModel.ssdl|res://*/Models.GoBagHomeModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\sqlexpress;initial catalog=GoBagHomeCom;Persist Security Info=True;User ID=xx;Password=xxxx;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
And my dbcontext
public partial class GoBagHomeComEntities : DbContext
{
public GoBagHomeComEntities()
: base("name=GoBagHomeComEntities")
{
}

Check dbo.__MigrationHistory if you have records means EF is trying to migrate, so truncate the table and try it again.
if the same thing repeated so you might need to check EF weather it migrates as settings.
have a look to this answer this, you need to have the connection string in web.config file, I think this is the only missed thing.

It seems like the problem is solved :)
I'm sure Owin was the bad guy - I was only using it for creating the authentication cookie. Nothing about tables.
After remove all Owin related code on the site it's running.

If your bring any change in your database you should follow the following steps:
Update the .edmx file. To update it double click on .edmx when it will open, please right click on a blank space of this file and select "update model from database".
Save and close the file.
Build the project.
Copy .dll file from bin folder and past it where you have hosted your application.

Related

Failing to get EF connection string to work when setup in Azure's management portal?

I am using ASP.NET MVC4 deployed on WebApps, EF6, Azure SQL Database
I have two connectionstrings, one direct to the DB and one that is needed as I am using Entity Framework. If I setup the first via Azure and leave the EF string in web.config, all works fine. Once I try to move the EF connectionstring to Azure, it breaks.
I have tried to follow all the posts on how to setup an EF connection string in Azure Management Portal, and the advice seems to be to use the following:
In Management Portal:
Name:
EFConnectionString
Value:
metadata=res://MyModel/model.csdl|res://MyModel/model.ssdl|res://MyModel/model.msl;provider=System.Data.SqlClient;provider connection string="Data Source=tcp:myserver.database.windows.net,1234;Initial Catalog=mydatabase;User ID=Admin#myserver.database.windows.net;Password=12345678"
SQL Database:
Custom
Note:
" is replaced by " in the Azure string.
In Web.config I still need the following:
<add name="EFConnectionString" connectionString="" providerName="System.Data.EntityClient"/>
I have changed certain details in the connectionstring for security reasons, but the true string does work fine in Web.config.
However when I do attempt the above, I get :
The page cannot be displayed because an internal server error has occurred.
Further Log error message:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
Any help appreciated. I am trying to do this to enhance the security of stored passwords for accessing DB on Azures, as I understand that setting them up via the portal encrypts them.
Thanks.
First check your deployment. I've run in a similar problem, and after a couple of hours struggling I found out that I was deploying without actually sending connection strings, falling back to whatever was in the portal config... the others keys in the we.config were being sent over, but the conn strings weren't... after I properly set it up on the portal, everything went back to normal...

Web app with EF + database first not working on Azure

I have a ASP.NET MVC application which uses EF (v6) as data access layer. My application works fine on IIS Express and also when deployed to the server running IIS 7.5.
The problem is that I'm getting the following exception when I deploy it to Azure (Web Sites).
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.
I've searched the web and I made sure my connection string starts with "metadata=" + checked my db context class' constructor to be sure it contains the correct name (in my case it's "name=PsDataEntities"))
My connection string looks like this: <add name="PsDataEntities" connectionString="metadata=res://*/PsDataModel.csdl|res://*/PsDataModel.ssdl|res://*/PsDataModel.msl;
provider=System.Data.SqlClient;
provider connection string="data source=SERVER_NAME;initial catalog=DB_NAME;user id=UID;password=PWD;
MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Anybody had the same problems?
Any help is appreciated
I did a few more tests (like changing metadata in the connection string to use fully qualified names instead of '*') without success, then I decided to set my metadata (regarding .csdl/.ssdl/.msl) programatically with the help from this post. Application now works correctly when deployed to Azure.

SQL Server Express and Entity Framework

I'm new to SQL Server Express (not to SQL Server in general) and I have a problem. I'm using Entity Framework (6-beta) in an ASP.NET MVC application.
I created a database using the SQL Server Management Studio. I can connect to the DB using Management Studio and my local Windows user account.
I dind't change the default configuration, so the SQL Server stores the data files in :
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA
The .mdf file of my database is stored in this folder.
My problem is: When I start the MVC application from Visual Studio, Entity Framework failes to open a connection to the database. The exception text is quite unprecise, but the inner exception reveals that it tries to connect to:
C:\Users\<MyUser>\Workspaces\<Solution>\<Project>\App_Data\<Context>.mdf
My connection string is:
"Data Source=.\SQLEXPRESS; Initial Catalog=<Name>; User Instance=False; Integrated Security=True;"
Does anyone know why Entity Framework tries to connect to this file? What can I do?
You need a web.config entry on the MVC application project (in case the DBContext is in another referenced project).
The web.config should have an entry like the one below in the connectionstrings section (note YOURCONTEXTNAME should match the name of your DBContext class).
<add name="YOURCONTEXTNAME" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=YOURDATABASENAME;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
Ok. I found the problem. I edited the wrong web.config file by accident.
Asp.Net MVC 5 creates a second web.config file in the Views folder.
Of course, the connection string must be inserted in the web.config of the root folder.

Database Connection Error with ef-code-first

I'm new to ef code first and have just used the reverse engineer code first to create a model of an existing database on Microsoft SQL Server 2008.
The problem I'm having is that even though I'm providing User ID and Password in the connection string, it's giving me an authentication error while complaining about my computer name as if I were using Integrated Security (which I'm not.)
The error I get is this:
Cannot open database \"edmTestDBContext\" requested by the login. The login failed.\r\nLogin failed for user 'jwelty-thinkpad\jwelty'.
My connectionString is this:
Data Source=srv-123;Initial Catalog=edmTestDB;Persist Security Info=True;User ID=user;Password=userpass;MultipleActiveResultSets=True
It seams to me like it's ignoring my User ID and using my machine name instead.
It's interesting that the connection string was auto generated by the Entity Framework tool and it worked for building the model but not for actually connecting the model back to the source database.
Any thoughts on what's going on?
I do have full permissions with my username/password as this is what I use with Sql Server Management Studio and that's also how I created the database in the first place.
I tried adding "Integrated Security=False;" and that was no help.
It looks like EF isn't finding your connection string. Make sure that it is in the config file being used (you might need to copy it from the class library config to the application config) and that it either has the same name as the context class or that you provide DbContext with the name by calling the appropriate base constructor. For example:
public EdmTestDBContext()
: base("name=MyConnectionStringName")
{
}
There are some built-in conventions in EF Code-first such as using the name of derived context class from DbContext to find the related connection string in the .config file.
So if your context class is named BlogContext, it will look for the following connectionString first:
<connectionStrings>
<clear />
<add
name="BlogContext"
...

How to show database in app_data when developing for azure with entity framework code first?

I am using the code first method in my windows azure project. I would like to see wich tables are created by the framework but the mdf file is not listed in the app_data folder.
I tried editing the default connection string to:
<add name="ModelContext" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|ModelContext.mdf;User Instance=true;Database=ModelContext"
providerName="System.Data.SqlClient" />
When using this connection string I get the following error:
CREATE FILE encountered operating system error 5(failed to retrieve text for this error. Reason: 15105) while attempting to open or create the physical file 'C:\Users\Myname\WindowsAzureProject1\MvcWebRole1\App_Data\ModelContext.mdf'.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
How can I solve this?
Any help is greatly appreciated.
This isn't the best way to do things. It is probably a permission problem. Take a look at this:
http://www.windowsazure.com/en-us/develop/net/tutorials/web-app-with-sql-azure/
It uses code first and works with Azure and starts out using SQL Express.
Sql Express is not installed in a web role. You need to use a Sql Azure database or add a start up task for the web role to install sql express. Take a look at this post from Steve Marx http://blog.smarx.com/posts/introduction-to-windows-azure-startup-tasks on how to add a start up task