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
Related
I am trying to retrieve data from an SQL Server database table in my MVC application. I have the following connection string:
<add name="EmployeeContext" connectionString="Data Source=localhost;Initial Catalog=SampleHR;Integrated Security=True"
providerName="System.Data.SqlClient"/>
But this gives me the following error:
Cannot open database "SampleHR" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\DefaultAppPool'.
On this line in my ActionResult code:
Employee empPersonal = epcontext.Employees.Single(emp => emp.EmployeeID == eid);
I tried the following two connection string:
<!-- <add name="Employee_PersonalContext" connectionString ="server=MANASI-HP\MSSQLSERVER; database=SampleHR; integrated security=SSPI;"
providerName="System.Data.SqlClient"/> -->
<!-- <add name="Employee_PersonalContext" connectionString ="server=MANASI-HP\MSSQLSERVER; database=SampleHR; username=dbo; password=;"
providerName="System.Data.SqlClient"/> -->
But these two gave me the error: "The underlying provider failed to open."
I have SQL Server 2012, with Windows Authentication. I ran the query "select CURRENT_USER" and got the username "dbo". I have not set any password and I am using Windows Authentication. I am also running the application on IIS server.
What could be the problem? Please let me know if I have to add any code here.
Edit: I went to project properties and changed the server to SQL Server instead of IIS and it works with this connection string:
<add name="EmployeeContext" connectionString="Data Source=localhost;Initial Catalog=SampleHR;Integrated Security=True;User ID=IIS APPPOOL\DefaultAppPool;Password="
providerName="System.Data.SqlClient"/>
but not with this one:
<add name="MvcDemoConnectionString" connectionString="Server=MANASI-HP;Database=SampleHR;Integrated Security=true"
providerName="System.Data.SqlClient"/>
I am confused.. :(
Edit 2 (11th Nov): I did not make any changes to IIS Manager right before I started with the MVC application in .NET. Just followed the instructions in the tutorial. I found this page about IIS.
IIS Data Source Settings
Do I need to do any of these things before I start building my application on IIS?
Dec 5th 2015:
I followed the instructions on these two pages:
ASP.NET Web Deployment
and
Deploy Database with IIS
Still not working.
If you are running the app in IIS and your connection string uses integrated security, the user it is going to use is whatever the app pool is running as. You can either change the user the app pool is running as, or provide the username and password for the windows authentication user you want to use in your connection string (something like Data Source=localhost;Initial Catalog=SampleHR;Integrated Security=True;User ID=username;Password=password
Look like you have hosted application in IIS and allow anonymous access, and your connecting string accepting windows authenticated user.
Whenever you have this type of setting in place, I'll recommend to run IIS application pool under service account instead of AppPoolIdentity, and make sure your SQL Server allow service account user.
For testing go to Application Pool -> advance settings -> and change 'Identity' with you network/windows credential. if your account have access to sql server your problem will be solve for a moment.
Note: I'll not recommend to keep running your application under personal account. Because once you change your password you have to change the App pool Identity again.
If you don't change, each app pool has it's own identity. In your case, just add a new user to your database SampleHR with the name IIS APPPOOL\MyAppPool using SQL Management Studio. You find the users list in the "Security/Users" subnode of your database.
I just had to grant the user permissions in SSMS. I just went back to default settings in ISS, granted permission to the user in SSMS, and that was it.
Does anyone have any other solution?
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.
Im working on a MVC Application and so far ive been using localDB since that was which was included in the tutorial. I want to switch the application now to my SQL Server but im not sure how to.
I get that I have to change the connection String. But not in what way exactly since the SQL Server has a username and password which my localDB doesnt.
Another question regarding this is, do I have to create the tables myself in the beginning on the SQL Server or will they be generated by the entity framework like in my localDB?
Current connection string for localDB:
<add name="AcquisitionDBContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Acquisitions.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
Im pretty new to this so any help is very much appreciated
if you are going to use MS SQL then it wil look something like this:
<add name="AcquisitionDBContext" connectionString="Data Source=hostname\sql_instance_name;Initial Catalog=databaseName;Persist Security Info=True;User ID=username;Password=password;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
Where "hostname\sql_instance_name" is the servername(or ip)/the sql instance name eg. localhost\sqlexpress
ConnectionStrings.com is the best reference for connection-string syntax. It shows you the options to use when you have a username+password. In this case:
Server=myServerAddress; Database=myDataBase; User Id=myUsername; Password=myPassword;
Entity Framework will not create your tables for you unless you tell it to by calling something like this:
Database.SetInitializer( new DropCreateDatabaseIfModelChanges<YourDbContextHere>() );
(Where Database is the System.Data.Entity.Database type).
Try it:
<connectionStrings>
<add name="<<Name of Connection String>>" connectionString="Data Source=<<Put here path to your SQL SErver>>;Initial Catalog=<<Database name>> ;Integrated Security=SSPI;Trusted_Connection=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
Here's how connection string should look like (tweak to your needs)
<add name="DefaultConnection" connectionString="Data Source=server_name,port; Initial Catalog=your_catalogue_name; Integrated Security=False; User ID=userName;Password=Passwd; MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
Before first run I would recommend using migrations.
Go to PackageManagerConsole (if you don't have it in menu, install NuGet first via Extension and Updates)
Then inside console type Enable-Migrations (if you didn't already).
Next type Add-Migration your_migration_name and then Update-Database to let migrations create tables for you.
If you change something in your model files later, you can again type Add-Migration your_migration_name and Update-Database to update database with those changes.
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" />
I just want to quickly spin up the default database in my development environment.
How is the easiest way to get around this problem?
I once faced this problem and resolved it. The key is using SQL authentication instead of Windows'. This is the clearest way to specify the default db.
Try connection string in this way:
<add name="MFCConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MFC.mdf;Initial Catalog=MFC;Integrated Security=false;User ID=sa;Password=123"
providerName="System.Data.SqlClient" />
remember to set default db of sa from master to MFC, and Integrated security = false.
BTW, sa is normally disabled, so enable and test it in sql server management studio first.
Run your application under account which has permission to create database on your development SQL server. If you are using SQL authentication specify credentials for SQL login in your connection string which has this permission. By default admin account specified during SQL server installation has this permission but you can add it to other logins as well.
This may be of use to anybody stumbling across this question, as I did, when looking for an answer to the error. These steps should be all you need and I've copied code in you can paste to get it running quickly.
I'm using Code First, tried using 'create-database' but got the error in the title.
Closed and re-opened (as Admin this time) - command not recognised but 'update-database' was so used that. Same error.
Here are the steps I took to resolve it:
1) Opened SQL Server Management Studio and created a database "Videos"
2) Opened Server Explorer in VS2013 (under 'View') and connected to the database.
3) Right clicked on the connection -> properties, and grabbed the connection string.
4) In the web.config I added the connection string
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=MyMachine;Initial Catalog=Videos;Integrated Security=True" providerName="System.Data.SqlClient"
/>
</connectionStrings>
5) Where I set up the context, I need to reference DefaultConnection:
using System.Data.Entity;
namespace Videos.Models
{
public class VideoDb : DbContext
{
public VideoDb()
: base("name=DefaultConnection")
{
}
public DbSet<Video> Videos { get; set; }
}
}
6) In Package Manager console run 'update-database' to create the table(s).
Remember you can use Seed() to insert values when creating, in Configuration.cs:
protected override void Seed(Videos.Models.VideoDb context)
{
context.Videos.AddOrUpdate(v => v.Title,
new Video() { Title = "MyTitle1", Length = 150 },
new Video() { Title = "MyTitle2", Length = 270 }
);
context.SaveChanges();
}
I have the same problem with EF 6.0 and code first. If you have multiple projects with different connection strings and running update-database from the package manager console even if you select the right default project, Visual studio reads the connection string from the start up project and so if there is no connection on that start up project then the error is permission denied..
You can solve it by set the right project as start up project (just for updte database).