I have a asp.net core 3.1 code first project in Visual Studio 2019 and SQL Server 2017
It gives this exception when I make a request to the database: Cannot open database "DbName" requested by the login. The login failed. Login failed for user 'sa'.
It was in Log SQL server: Login failed for user 'NT Service\SSISScaleOutMaster150'. Reason: Could not find a login matching the name provided. [CLIENT: ]
I tried most of the solutions on the internet but none of them worked
Other info: Named pipes are enabled TCP/IP is enabled Remote connections are allowed I even removed the SQL server and reinstalled it and update Visual studio I tried this and this but didn't work
when i create database manually the exception changed to :Invalid object name 'User'.
connection string:
"myconn": "Server=.;Database=onlineShopDB;User Id=sa;password=qazwsxedc;Trusted_Connection=False;MultipleActiveResultSets=true;"
},
configure service:
{
services.AddMvc();
services.AddDbContext<OnlineShopContext>(item => item.UseSqlServer(Configuration.GetConnectionString("myconn")));
services.AddScoped<IDataRepository<User>, UserRepository>();
services.AddControllersWithViews();
}
repositoty class:
public class UserRepository : IDataRepository<User>
{
private readonly OnlineShopContext _onlineShopContext;
public UserRepository(OnlineShopContext onlineShopContext)
{
_onlineShopContext = onlineShopContext;
}
public void Add(User entity)
{
_onlineShopContext.Users.Add(entity);
_onlineShopContext.SaveChanges();
}
}
IDataRepository:
public interface IDataRepository<TEntity>
{
IEnumerable<TEntity> GetAll();
TEntity Get(long id);
void Add(TEntity entity);
void Update(TEntity dbEntity, TEntity entity);
void Delete(TEntity entity);
}
onlineShopContext:
public class OnlineShopContext:DbContext
{
public OnlineShopContext(DbContextOptions options):base(options)
{ }
public virtual DbSet<UserType> UserTypes { get; set; }
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<ProductFeatures> ProductFeatures { get; set; }
public virtual DbSet<ProductImages> GetProductImages { get; set; }
public virtual DbSet<OrderStatus> OrderStatuses { get; set; }
public virtual DbSet<Order> Orders { get; set; }
public virtual DbSet<OrderItems> OrderItems { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserType>().HasData(
new UserType() { Id = 1, Name = "User", Title = "a" },
new UserType() { Id = 2, Name = "Admin", Title = "b" });
}
}
exec sp_readerrorlogand include it in your question