Last active
June 25, 2016 12:58
-
-
Save advanceboy/7e365b17fb86c9458ab5706a0b5baae3 to your computer and use it in GitHub Desktop.
DI を使って、 Entity Framework Core (SQLite) をコンソールアプリで実行するサンプル。 (.NET 4.5.1, .NET Core 両対応) Nuget から、以下のパッケージを Install-Package する必要がある Microsoft.Extensions.Logging.Debug Microsoft.Extensions.Logging.Console Microsoft.Extensions.Configuration.Json Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.Sqlite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"ConnectionStrings": { | |
"DefaultConnection": "Filename=Blogging.db" | |
}, | |
"Logging": { | |
"IncludeScopes": false, | |
"LogLevel": { | |
"Default": "Debug", | |
"System": "Information", | |
"Microsoft": "Information" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.EntityFrameworkCore; | |
using System.Collections.Generic; | |
public class BloggingContext : DbContext { | |
public DbSet<Blog> Blogs { get; set; } | |
public DbSet<Post> Posts { get; set; } | |
public BloggingContext(DbContextOptions<BloggingContext> options) : base(options) { } | |
} | |
public class Blog { | |
public int BlogId { get; set; } | |
public string Url { get; set; } | |
public List<Post> Posts { get; set; } | |
} | |
public class Post { | |
public int PostId { get; set; } | |
public string Title { get; set; } | |
public string Content { get; set; } | |
public int BlogId { get; set; } | |
public Blog Blog { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.EntityFrameworkCore; | |
// Nuget から、以下のパッケージを Install-Package する必要がある | |
// Microsoft.Extensions.Logging.Console | |
// Microsoft.Extensions.Configuration.Json | |
// Microsoft.EntityFrameworkCore.Tools | |
// Microsoft.EntityFrameworkCore.Sqlite | |
public class Program { | |
public static void Main(string[] args) { | |
var configuration = new ConfigurationBuilder() | |
.SetBasePath(System.IO.Directory.GetCurrentDirectory()) | |
.AddJsonFile("appsettings.json") | |
.Build(); | |
var services = new ServiceCollection(); | |
services | |
.AddLogging() | |
.AddDbContext<BloggingContext>(options => { | |
options.UseSqlite(configuration.GetConnectionString("DefaultConnection"), sqliteOptions => { | |
}); | |
}); | |
var serviceProvider = services.BuildServiceProvider(); | |
serviceProvider.GetService<ILoggerFactory>() | |
.AddConsole(configuration.GetSection("Logging")); | |
using (var db = ActivatorUtilities.CreateInstance<BloggingContext>(serviceProvider)) { | |
db.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/adonet" }); | |
var count = db.SaveChanges(); | |
Console.WriteLine("{0} records saved to database", count); | |
Console.WriteLine(); | |
Console.WriteLine("All blogs in database:"); | |
foreach (var blog in db.Blogs) { | |
Console.WriteLine(" - {0}", blog.Url); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
説明のコメントに、 不要なパッケージをインストールするように書かれていたので、削除