1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

asp.net core > entity-framework-core-dbcontext (作成中)

Last updated at Posted at 2018-08-21

SampleContext側に入れるのが良いのか、
App側で設定を行うのが良いのか、悩み中
ベストプラクティスを教えてほしい。

Configuration.GetConnectionString("BloggingDatabase")

ASP.NET Core の構成 | Microsoft Docs

appsettings.json

appsettings.json
{
  "ConnectionStrings": {
      "BloggingDatabase": "Server=yourip; Database=yourdbname; User Id=yourusername; Password=yourpassword; Pooling=true;",
      "Db2ConnectionString": "Database=yourdbname;UserID=yourusername;Password=yourpassword;Server=yourip:yourport",
      "SomeOtherKey": "SomeOtherValue"
  }
}

DB Contextファイルの編集

SampleContext

AppSettingsJsonクラスの追加
SampleContext.csの先頭らへんに以下を追加

SampleContext.cs
public static class AppSettingsJson
{
    public static string ApplicationExeDirectory()
    {
        var location = System.Reflection.Assembly.GetExecutingAssembly().Location;
        var appRoot = Path.GetDirectoryName(location);

        return appRoot;
    }

    public static IConfigurationRoot GetAppSettings()
    {
        string applicationExeDirectory = ApplicationExeDirectory();

        var builder = new ConfigurationBuilder()
        .SetBasePath(applicationExeDirectory)
        .AddJsonFile("appsettings.json");

        return builder.Build();
    }
}
SampleContext.cs

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

    if (!optionsBuilder.IsConfigured)
    {
        var Configuration = AppSettingsJson.GetAppSettings();
        var connectionString = Configuration.GetConnectionString("BloggingDatabase");
        optionsBuilder.UseSqlServer(connectionString);
    }
}

SampleServiceTest

SampleServiceTest.cs

[TestClass]
public class SampleServiceTest
{

    private SampleContext context;
    private static IConfiguration Configuration { get; set; }

    public SampleServiceTest()
    {
        //var builder = new ConfigurationBuilder()
        //    .SetBasePath(Directory.GetCurrentDirectory())
        //    .AddJsonFile("appsettings.Development.json");

        //Configuration = builder.Build();
        //var connectionString = Configuration.GetConnectionString("BloggingDatabase");

        //var optionsBuilder = new DbContextOptionsBuilder<SampleContext>();
        //optionsBuilder.UseSqlServer(connectionString);
        //context = new SampleContext(optionsBuilder.Options);
        
        context = new SampleContext();
    }


    [TestMethod]
    public void GetByIDAsync_Test()
    {
        var id = 1;
        var result = SampleService.GetByIDAsync(context, id).Result;
        Assert.IsNotNull(result);
    }

}

Entity Framework Core DbContext

DbContext - EF Core の構成 | Microsoft Docs

c# - .NET Core get connection string from appsettings.json - Stack Overflow

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?