Sql or Connection string issue ? asp.net mvc 4 tutorials - c#

Hi I'm having a continuous issues when it comes to database creation and access when trying out mvc 4 tutorials. The latest for example http://www.asp.net/mvc/tutorials/mvc-4. I've got to the point where I'm trying to use EF Code First Migrations to migrate some changes to the model classes so the change is applied to the database. And dummy data seeded to the database.
When I run "update-database" after entering "add-migration Initial" I get ....
Cannot open database "_Movies.mdf" requested by the login. The login failed.
Login failed for user 'USER\me'
When I check SQL server management studio, my name is listed in the Logins folder, within Security folder. Is there anything else I can check ? Still learning, so please explain answers.
Connection string if needed...
<add name="MovieDBContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=_Movies.mdf;AttachDbFilename=|DataDirectory|\_Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

Related

connection asp.net project to azure database fail

Trying to connect ASP.Net code first project to Azure. At this stage it launching application online as project was published. I can browse pages, but when I try to create new dynamic pages it gives me an error. My project works fine locally.
I uploaded project using Azure SDK and used info from azure such as the server name and connection string. Here it is:
<connectionStrings>
<add name="PhClub" connectionString="Server=tcp:phclub.database.windows.net,1433;Initial Catalog=PhClub;Persist Security Info=False;User ID=myloginname for azure;Password=******;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" />
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-PhClub-20170304023453.mdf;Initial Catalog=aspnet-PhClub-20170304023453;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
However, there was one thing which cached my eye but i don't know what it means and how to fix it. In App_data folder i have two databases, but in the publish window I see three of them:
As you can see something is wrong with last one.
Here how it looks in preview window:
Looking for your advise since this is my first solo project. Thank you in advance!
Try adding providerName="System.Data.SqlClient" at the end of the PhClub connection string.
If we create <customErrors mode="Off"/> in our web.config file, then we could see the details error message. We could get more info about how to troubleshoot a web app in Azure App Service using Visual Studio
We could debug it with following 2 ways:
1.Make sure that the connectionstring for dbcontext is the azure sql connectionstring
2.Make sure that Azure sql could be accessed by azure service.(Allowed default). More detail info about azure sql firewall please refer this document.
Try to ignore the # symbol with \ before or double it like ##.
Because with azure # separates loginName#server.
An alternative is to create a new user/login in the database, which's username doesn't contain a # and use this user in the connection string.
Thanks for all answers. It seems that problem was in connection string login credentials, i deleted web app, server and database, recreated it again with new simple login name. Changed all of the login name and password in connection strings. This way i got rid of the error.

Entity Framework MigrateDatabaseToLatestVersion CREATE DATABASE permission denied in database 'master' error

I've been looking through questions on here but have yet to come across anything that deals with my problem, apologies if this is a duplicate and I've missed something.
I have an ASP.Net MVC project with Entity Framework Code First.
In my Dbcontext I have;
public Context() : base("DatabaseName")
{
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>());
}
In my web config I have;
<connectionStrings>
<add name="DatabaseName" connectionString="connsection string is here"/>
</connectionStrings>
When my application is deployed, I get the error
'CREATE DATABASE permission denied in database 'master'.' as soon as
it tries to access the Context for the first time.
What I don't understand is how to stop it trying to create a database and just use the connection string that I have given it. The database already exists (It's on shared hosting so I don't have much control over it) and I know the connection string is correct as I have Hangfire installed in the same application, using the same connection string and it has successfully created the tables but for some reason EF doesn't create the tables in the database and instead tries to create one, which it cannot do.
Entity Framework is installed in the same project as the config file, in fact, it's a single project application.
I've tried creating another database and adding a second connection string in case Hangfire was preventing EF from using it but had the same issue. I've also tried putting the full connection string directly into :base("") on the context, but it has no effect.
try getting connectionString by name
public Context()
:base("name=DbConnection")
{
}
in Web.config file add
<connectionStrings>
<add name="DbConnection" connectionString="Data Source=serveraddress;Persist Security Info=False;User ID=usename;Password=pass;Initial Catalog=databasename" providerName="System.Data.SqlClient" />
</connectionStrings>

EF6 forcing the use of my integrated account

I'm trying to use "Update-database" from package Manager console, i always get the error Login Failed for user 'TEMP\IGIT02T. although this use is defined as SA and DB owner
I found many articles and similar questions but didn't solve my issue.
What i did, i created an account in sql server "userID:testUser password:Test#2016" changed my connection string and set Integrated Security= false.
Now if i run PM> Update-Database i still get error
Error Number:18456,State:1,Class:14 Login failed for user 'TEMP\IGIT02T'
I tested the following connection strings:
<add name="TestDB" connectionString="Data Source=XXT0006\MSSQLServerDv;User Id=testUser;Password=Test#2016; Initial Catalog=TestDbEf" providerName="System.Data.SqlClient" />
add name="TestDB" connectionString="Data Source=XXT0006\MSSQLServerDv;User Id=testUser;Password=Test#2016; Initial Catalog=TestDbEf;Integrated Security=false" providerName="System.Data.SqlClient" />
In my DbContext class
public TestEFDBContext()
: base("TestDB")
{
Database.SetInitializer<TestEFDBContext>(new DropCreateDatabaseAlways<TestEFDBContext>());
}
why it doesn't pick the local sql login, also if i try to login directly to SQL server using testUser or TEMP\IGIT02T I have no issue and i can CRUD create and delete databases.
Thank you

Changing ConnectionString authorization method cause migration fail

I've created project to read from MSSQL DB and display some data. VS's wizards created connection string like the following:
<add name="SomeContext" connectionString="data source=xxx.xxx.xxx.xxx\_name,port;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
When I tried to deploy application to the real web server I changed connection string to use faceless DB account:
<add name="SomeContext" connectionString="data source=xxx.xxx.xxx.xxx\_name,port;User Id=userid;Password=password; MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
It looks like EF thinks that there some migration needed and tries to execute a lot of meaningless operations including attempts to execute SQL expressions with schema creating.
I have no write permissions on this DB server and I don't see any reasons to allow EF change DB structure.
So I'm completely lost there. What is the proper way to change authorization methods without changing on DB side?
Try to change connection string to production in VS project, then use Update-Database via Package Manager Console.
Pretty strange for me but the root of the issue was in default "initial catalog". It was stored somewhere for connection with integrated security but when I changed authorization method default was broken. As soon I defined initial catalog it was fixed.

How to get the ASP.NET MVC 4 model to link to an Oracle database

This is my first MVC application so please be patient and kind. I have created an ASP.NET MVC 4 Web Application in Visual Studio 2012. I created the database code first, so all I did was create the model and a data context and a SQL server database was created for me. Every time I changed the model I could just update the database. I also connected to an Oracle database get other data that I needed. These are the connection strings that were used:
<connectionStrings>
<add name="ApplicationDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=something;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="ConnectionString" connectionString="Data Source=something;Persist Security Info=True;" providerName="Oracle.DataAccess.Client" />
</connectionStrings>
However, the database related to the model needs to be in Oracle (and I need a script for it but that is next weeks question) not SQL.
I was hoping that I could just change the data context provider name in the web config like so:
<connectionStrings>
<add name="ApplicationDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=something;Integrated Security=True" providerName="System.Data.OracleClient" />
<add name="ConnectionString" connectionString="Data Source=something;Persist Security Info=True;" providerName="Oracle.DataAccess.Client" />
</connectionStrings>
But that is not working - it gives me errors.
Please note that I am NOT asking about OracleMembershipProvider. This question is unique in that I want the model to relate to an Oracle database. I am not worried about membership and user authentication etc. I have already taken care of that.
It seems like it should be simple enough, but I have googled until every link I find is purple.
I need you to tell me the steps and what to do (hopefully in the web config) to get my model to 'create' an Oracle database. Please.
After reading through a ton of articles I found out that Oracle doesn't support code first (yet) and although there are some work-a-rounds it’s probably best to create scripts to create the db for us.
So as an alternate solution we to created the database in Oracle first and then connected to it from the MVC application.
We added an ADO.NET Entity Data Mode and in the EDM Wizard we said "Generated from database" and added a new connection to the Oracle database. You need Oracle Developer Tools for Visual Studio for this.
Add a new controller and for the data context class, choose Entities. Don't forget to add the connection string to the web config.
<add name="Entities" connectionString="...;provider=Oracle.DataAccess.Client;provider connection string=...;data source=...;" providerName="System.Data.EntityClient" />
This should be done before you start coding because you no longer have a model so I have had to change my code quite a lot. Not more than a day or two's work though.

Categories