1

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" });

            
        }  
    }
12
  • Please check the error log on SQL server exec sp_readerrorlog and include it in your question Commented Jul 24, 2020 at 7:01
  • Just checking, can you definitely access the database using those sa credentials via SSMS? Commented Jul 24, 2020 at 7:04
  • yes, I can login to SSMS with the 'sa' account and windows authentication Commented Jul 24, 2020 at 7:17
  • the error log on sql server is : Login failed for user 'NT Service\SSISScaleOutMaster140'. Reason: Could not find a login matching the name provided. [CLIENT: <local machine>] Commented Jul 24, 2020 at 7:28
  • 1
    thank allmhuran, your link was useful and solved my problem Commented Jul 24, 2020 at 9:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.