I have a ASP.NET MVC website with Entity Framework 6.0 and "database first" approach. Everything was working fine until my web host upgraded to MSSQL Server 2016 SP1. Now the website tries to recreate the database, it seems beacause I'm getting this error:
System.Data.SqlClient.SqlException: CREATE DATABASE permission denied in database 'master'.
Nothing else has changed as far as I know. On my dev machine I was able to do the same database upgrade (from 2014 to 2016 SP1) and it still works fine.
I have no migrations-folder and the __MigrationHistory table is empty.
Any suggestions on what to try?
Thanks!
Related
I have gone through this ASP.Net MVC Core tutorial at:
https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/working-with-sql.
And I am able to use dotnet migrations to create the database against the localDb with this connection string in the appsettings.json file:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-MvcMovie-3005e0f3-42f4-4163-94cc-f794a05e8b91;Trusted_Connection=True;MultipleActiveResultSets=true"
}
Now I want to point it to SQL Server Express to see if I can work from there and SSMS.
So now I use this in the appsettings.json file:
{
"ConnectionStrings": {
"SqlExpressConnection": "Server=.\\SQLEXPRESS;Database=MvcMovieDb1;Integrated Security=True;MultipleActiveResultSets=true"
}
And in StartUp.ConfigureServices(), change:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
to
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("SqlExpressConnection")));
Then in the command prompt run:
dotnet ef database update
But now I get:
"CREATE DATABASE permission denied in database 'master'.
How can I give ef permission to create the database?
Is there a login/password parameter that the dotnet ef can use if I create a login with CREATE Permissions?
Well it seems I don't have DB permissions like I thought.
I just did a test to add a db from within SSMS and I don't seem to have CREATE privilege
"CREATE DATABASE Permission denied in database 'master'.
I am in SSMS under my computer admin login account.
And I startup SSMS using windows authentication.
So the top connection node looks like:
FullComputerName\SQLExpress(SQL Server 11.0.6020 - CompanyDomainName\Samuel).
The Windows Authentication login is in the form: CompanyDomainName\Samuel.
I don't see my CompanyDomainName\Samuel account under Computer Manager - Users.
Not sure how I am able to login to the computer with this account.
How can I add this account as a login under Security Logins?
Your local Windows account CompanyDomainName\Samuel is the SQLExpress administrator. It means you are admin of SQLExpress when you are logged on Windows.
The Integrated security=true is used to connect to SQL Server with the Windows account, but it doesn't work because IIS (or Kestrel or any web server) will use a different Windows user account.
You have two options :
add the Windows user account of you web server in Security/Logins, and check Windows authentication
or create a new login with SQL Server authentication, and update you connection string to use it instead
"Server=.\\SQLEXPRESS;Database=MvcMovieDb1;User Id=dbUserName;Password=thePassword"
(note : if you choose this last solution, you should be aware of potential security issues if you store password in your source code. Read this page for more information)
I was never going to have priveledges since who ever built this laptop and installed SQL Server (2012) did it under their account. So I installed SQL Server Express Advances 2014 and SSMS 2014.
Now I can create DBs directly in SSMS 2014.
In appsettings.json I created this second connection string and updated it in Startup.cs to reflect the "SQLEXPRESS2014" conn string.
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-MvcMovie-3005e0f3-42f4-4163-94cc-f794a05e8b91;Trusted_Connection=True;MultipleActiveResultSets=true",
"SqlExpressConnection": "Server=.\\SQLEXPRESS2014;Database=MvcMovie;Integrated Security=True;MultipleActiveResultSets=true"
}
Then I ran
>dotnet ef database update
Everything is good for me now.
I make a new website + SQL DB on my Azure portal.
I create a new MVC 4 internet project in Visual Studio 2013 Express for Web.
I compile and it runs perfectly in local environment.
I set Azure to log detailed error messages.
I download publishing profile from Azure and deploy.
I get the 500.0 error (Module: AspNetInitializationExceptionModule).
The thing that makes it even more wierd is that a project I have developed on for a longer period of time is deployed without problems - only when making a new website + DB on Azure the problem arises.
Hope that somebody can give me a hint.
Running this in Package Manager did the trick:
Update-Package Microsoft.AspNet.WebApi -reinstall
I got the hint from the following article after having read the eventlog:
Could not load file or assembly 'System.Web.Http 4.0.0 after update from 2012 to 2013
I have an application which uses IIS Server Express. I want it to use IIS on my pc.
My application is working with IIS Server Express perfectly but when I try to host on IIS I get th following error message:
"Migrations is enabled for context 'PSTContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console."
Anybody can help me?
I am developing an ASP.NET MVC website, which I want to host on Azure Websites. While in development, I have been using an MDF file in my App_Data directory with a connection string looking like this:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=MyApp;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MyApp.mdf;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
To try it out on Azure, I was hoping I could leave this connection string as is, and simply FTP my MyApp.mdf into the App_Data folder on Azure, since it is all set up with the example data I want to use. However, when I tried to access my site, I ran into the following error:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: SQL
Network Interfaces, error: 52 - Unable to locate a LocalDB
installation. Verify that SQL Server Express is properly installed and
that the LocalDB feature is enabled.)
My question is, is there a way I can run my Azure website connecting to an MDF file in my App_Data folder, or am I forced to use an Azure SQL database?
You can't use an .mdf file in App_Data, but you aren't forced to SQL Azure -- you can use SQL Server Compact. Deployment from LocalDB to Compact is easy if you are using Code First Migrations; otherwise you will have to migrate to SQL Server Compact before you deploy. If you decide to go with Compact you'll have to make sure the database engine gets deployed, and you can find instructions for that in this tutorial:
http://www.asp.net/mvc/tutorials/deployment/deployment-to-a-hosting-provider/deployment-to-a-hosting-provider-deploying-sql-server-compact-databases-2-of-12
You'll have to use SQL Azure to use the Websites/Cloud Service features.
If you haven't already you'll probably want to have a look at web.config transformations with web deploy to ease the publishing experience.
http://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx
http://www.hanselman.com/blog/TinyHappyFeatures3PublishingImprovementsChainedConfigTransformsAndDeployingASPNETAppsFromTheCommandLine.aspx
You can import your data into the SQL Azure DB via the management tools or if you're using SQL Server 2012 you can import/export data via the portal.
I'm a absolute newbie in MVC, and now I am standing the beginning of it.
I checked out the tutorial podcast, in which i got i can add a new item of SQL Server database under app_data. But once I click that, a msg popup to show that in my desktop, no SQL Server 2005 or 2008 Express was installed.
But in fact, I have a SQL Server 2005 Enterprise installed already. Why this happened? And how to add a SQL Server database if I got a enterprise version?
Thanks a lot.
Adding the database file to the app_data folder only works with SQL Server Express editions. There's nothing you can do to enable it for Enterprise versions.
If you have SQL Server Enterprise, you need to create your database and your db objects in that server (using SQL Server Mgmt Studio or Visual Studio) and connect to that server. You cannot put your MDF/LDF files into app_data with Enterprise version - it just won't work.
Or if you don't like this approach, you could always install SQL Server 2005/2008 Express on your machine and then use the "put your MDF file into app_data" approach with that Express edition.