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).
dotnet new console -o CodeFirstDemo
CodeFirstDemo directorycd CodeFirstDemo
dotnet run
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
CodeFirstDemo directory in VS codecode .
Posts.cs to the project via VS codeusing 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;}
    }
}
BlogDBContext.cs via VS codeusing 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;");
    }
}
Program.cs file as shown belowusing 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();
        }
    }
}
dotnet run
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?
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.
User contributions licensed under CC BY-SA 3.0