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

            
        }  
    }
Sara
  • 33
  • 6
  • Please check the error log on SQL server `exec sp_readerrorlog` and include it in your question – Preben Huybrechts Jul 24 '20 at 07:01
  • Just checking, can you definitely access the database using those sa credentials via SSMS? – Jamie Burns Jul 24 '20 at 07:04
  • yes, I can login to SSMS with the 'sa' account and windows authentication – Sara Jul 24 '20 at 07: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: ] – Sara Jul 24 '20 at 07:28
  • If you're using the 'sa' account in SSMS then you're not using windows authentication, you're using SQL authentication. – allmhuran Jul 24 '20 at 07:30
  • I mean, I tried both methods – Sara Jul 24 '20 at 07:36
  • Ah, I see what you mean. OK, when you log in as 'sa', using SQL authentication, are you able to switch to the onlineShopDB database? It's possible for the sadbo mapping to become disconnected, in which case connecting to master and running `alter authorization on database::onlineShopDB to sa` does the trick. – allmhuran Jul 24 '20 at 07:52
  • thank for your comment, i'm using code first approach and onlineShopDB database doesn't exist – Sara Jul 24 '20 at 08:15
  • Well, your connection string specifies it: `Database=onlineShopDB`. It sounds like you're not actually creating your entities? – allmhuran Jul 24 '20 at 08:27
  • See, for example, [here](https://stackoverflow.com/questions/49721742/ef-code-first-not-creating-the-database) – allmhuran Jul 24 '20 at 08:32
  • 1
    thank allmhuran, your link was useful and solved my problem – Sara Jul 24 '20 at 09:05
  • Does this answer your question? [EF code first not creating the database](https://stackoverflow.com/questions/49721742/ef-code-first-not-creating-the-database) – Preben Huybrechts Jul 27 '20 at 05:36

0 Answers0