.NET Core console application with EF Core is not working when run from using dotnet run command, throwing authentication failed exception

1

I wrote a simple .NET Core console application to explore EF Core using the code first approach and I followed the below steps (windows machine).

  1. Create a new .NET Core console application
dotnet new console -o CodeFirstDemo
  1. cd into the CodeFirstDemo directory
cd CodeFirstDemo
  1. run the application, which compile and produce the output
dotnet run
  1. Now to start Entity Framework Core, add the EF Core package for SQL Server
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
  1. Open the CodeFirstDemo directory in VS code
code .
  1. Add a new file called Posts.cs to the project via VS code
using System;
using System.Collections.Generic;
using System.Linq;

namespace CodeFirstDemo 
{
    public class Post 
    {
        public int PostId {get; set;}
        public DateTime DatePublished {get; set;}
        public string Description {get; set;}
        public string Body {get; set;}
    }
}
  1. Add another file called BlogDBContext.cs via VS code
using System;
using Microsoft.EntityFrameworkCore;
using System.Configuration;

namespace CodeFirstDemo 
{
    public class BlogDBContext : DbContext 
    {
        public DbSet<Post> Posts { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder options)
            => options.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=CodeFirstDemo;Trusted_Connection=True;");
    }
}
  1. Update the Program.cs file as shown below
using System;

namespace CodeFirstDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Creating dbcontext...");
            BlogDBContext dbContext = new BlogDBContext();

            Console.WriteLine("Creating post entry...");

            Post post = new Post()
            {
                // do not insert PostId because it is marked as Identity column
                DatePublished = DateTime.Now,
                Description = "Description.",
                Body = "Body."
            };

            Console.WriteLine("Adding post to posts dbset...");
            dbContext.Posts.Add(post);

            Console.WriteLine("Saving to database!");
            dbContext.SaveChanges();

            // wait for input to keep the console open
            Console.ReadLine();
        }
    }
}
  1. Now run the program
dotnet run
  1. The program fails with this SqlException (0x80131904):
Creating dbcontext...
Creating post entry...
Adding post to posts dbset...
Saving to database!

Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot open database "CodeFirstDemo" requested by the login. The login failed.

How to fix this error?

c#
.net-core
console
ef-core-3.0
dotnet-cli
asked on Stack Overflow Jul 4, 2020 by Rajkumar M • edited Jul 4, 2020 by marc_s

1 Answer

1

After jumping between different answers, I ended up at the below article, which helped me solve the SqlException (0x80131904).

https://docs.microsoft.com/en-us/ef/core/get-started/?tabs=netcore-cli#create-the-database

Run the below commands to fixed the issue.

dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet ef migrations add InitialCreate
dotnet ef database update

Output for migrations command:

Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'

Output for update command:

Build started...
Build succeeded.
Applying migration '20200704134852_InitialCreate'.
Done.

Check the Sql server for the creation of CodeFirstDemo database and run the application.

dotnet run 

Output:

Creating dbcontext...
Creating post entry...
Adding post to posts dbset...
Saving to database!

Goto the SQL Server and check the record successfully added to the CodeFirstDemo database.

Note: I used SQL Server Object Explorer in Microsoft Visual Studio Community 2019 to access SQL Server.

answered on Stack Overflow Jul 4, 2020 by Rajkumar M • edited Jul 4, 2020 by marc_s

User contributions licensed under CC BY-SA 3.0