0

I have created an application in .NET Core where I am trying with database first approach.

I was able to add Migration using "Add-Migration InitialCreate" but when I am trying to create a database in SQL Server using " Update-Database" I am getting the below error.

Login failed for user ''.

Below is my content from appsettings.json file

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DevConnection": "Server=(local)\\sqlexpress;Database=Mydatabase:Trusted_Connection=true;MultipleActiveResultSets=true;"
  }
}

Just FYI Also I was facing issue to create migration so I have added below code after that I was able to create migrations.

public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
{
    public MyDbContext CreateDbContext(string[] args)
    {
        IConfigurationRoot configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();
        var builder = new DbContextOptionsBuilder<MyDbContext>();
        var connectionString = configuration.GetConnectionString("DevConnection");
        builder.UseSqlServer(connectionString);
        return new MyDbContext(builder.Options);
    }
}

Notes:

  1. SQL Server Version: 18.4
  2. .NET Core Version: 3.1.2
  3. I am trying with windows authentication

2 Answers 2

0

Try to change the connection string to: "DevConnection": "Server=.\\SQLEXPRESS;Database=Mydatabase:Trusted_Connection=true;MultipleActiveResultSets=true;". In this way it works for me!

Sign up to request clarification or add additional context in comments.

2 Comments

Oh wait, isn't the problem that you are using "DefaultConnection" in the CreateDbContext function and that it is called "DevConnection" in the appsettings.json file?
Apology, i am using DevConnection not DefaultConnection in the CreateDbContext and that is calling my DevConnection in "appsettings.json" file. I just made the correction above.
0

Use for Database first Approach Entity Framework Core In ASP.NET Core MVC

Step 1:Add Packages in your project:

1.Add Microsoft.EntityFrameworkCore.Design 2.Add Microsoft.EntityFrameworkCore.SqlServer 3.Add Microsoft.EntityFrameworkCore.Tools

Step 2: Add Migration command in Package Manager Console:

 Scaffold-DbContext "Server=Server name;Database=Database name;User
 Id=User name;Password=password;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

Comments

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.