Asp.Net Core EF 数据模型创建数据库
时间:2021-07-01 10:21:17
帮助过:68人阅读
class OrderContext : DbContext
{
public OrderContext(DbContextOptions<OrderContext> options) :
base(options) { }
public DbSet<Order> Blogs {
get;
set; }
public DbSet<Post> Posts {
get;
set; }
}
public class Order
{
public int OrderId {
get;
set; }
public string OrderName {
get;
set; }
public ICollection<Post> Posts {
get;
set; }
}
public class Post
{
public int PostId {
get;
set; }
public string Title {
get;
set; }
public string Content {
get;
set; }
public int OrderId {
get;
set; }
public Order Order {
get;
set; }
}
View Code
通过依赖关系注入注册服务
public void ConfigureServices(IServiceCollection services)
{
var connection = @"Server=.;Database=Blogging;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<BloggingContext>(options => options.UseSqlServer(connection));
}
打开程序包管理器控制台分别输入
Add-Migration InitialCreate
Update-Database
Asp.Net Core EF 数据模型创建数据库
标签:ddb open ini services ram asp ons closed ISE