using Demo.Domain; using Microsoft.EntityFrameworkCore; namespace Demo.Data { class DemoContext:DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(connectionString: @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Demo;"); //base.OnConfiguring(optionsBuilder); } public DbSet<League> Leagues { get; set; } public DbSet<Club> Clubs { get; set; } public DbSet<Player> Players { get; set; } } }
准备类库:
using System;
using System.Collections.Generic;
namespace Demo.Domain
{
public class Club
{
public Club() {
Players = new List<Player>();
}
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public DateTime DateOffEstablishment { get; set; }
public string History { get; set; }
public League League { get; set; }
public List<Player> Players { get; set; }
}
}
namespace Demo.Domain
{
public class League
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
}
using System;
namespace Demo.Domain
{
public class Player
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime DateOfBirth { get;set; }
}
}
nuget包:
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" />
命令工具
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.4">
常用命令:
get-help EntityFrameworkCore
clear
Add-Migration xx
Drop-Database
Get-DbContext
Remove-Migration
Scaffold-DbContext
Script-DbContext
Script-Migration
Update-Database -verbose