PM> Install-Package Microsoft.EntityFrameworkCore.Tools -Pre
using Microsoft.EntityFrameworkCore; namespace Vs2015WinFormsEfcSqliteCodeFirst20170304Example.Model { public class Vs2015WinFormsEfcSqliteCodeFirst20170304ExampleContext : DbContext { public DbSet<City> Cities { get; set; } public DbSet<Person> People { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("Filename=Vs2015WinFormsEfcSqliteCodeFirst20170304Example.sqlite"); } } }
using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; namespace Vs2015WinFormsEfcSqliteCodeFirst20170304Example.Model { public class City { public int Id { get; set; } public string Name { get; set; } [InverseProperty("City")] public virtual ICollection<Person> People { get; set; } } }
using System.ComponentModel.DataAnnotations.Schema; namespace Vs2015WinFormsEfcSqliteCodeFirst20170304Example.Model { public class Person { public int Id { get; set; } public string Name { get; set; } public string Surname { get; set; } public int? CityId { get; set; } [InverseProperty("People")] public virtual City City { get; set; } } }
PM> Set-ExecutionPolicy RemoteSigned
PM> Add-Migration -Name "Vs2015WinFormsEfcSqliteCodeFirst20170304ExampleMigration" -Context "Vs2015WinFormsEfcSqliteCodeFirst20170304ExampleContext"
using Microsoft.EntityFrameworkCore.Migrations; namespace Vs2015WinFormsEfcSqliteCodeFirst20170304Example.Migrations { public partial class Vs2015WinFormsEfcSqliteCodeFirst20170304ExampleMigration : Migration { protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "Cities", columns: table => new { Id = table.Column<int>(nullable: false) .Annotation("Sqlite:Autoincrement", true), Name = table.Column<string>(nullable: true) }, constraints: table => { table.PrimaryKey("PK_Cities", x => x.Id); // table.UniqueConstraint("UQ_Cities_Name", x => x.Name); }); migrationBuilder.CreateTable( name: "People", columns: table => new { Id = table.Column<int>(nullable: false) .Annotation("Sqlite:Autoincrement", true), CityId = table.Column<int>(nullable: true), Name = table.Column<string>(nullable: true), Surname = table.Column<string>(nullable: true) }, constraints: table => { table.PrimaryKey("PK_People", x => x.Id); table.ForeignKey( name: "FK_People_Cities_CityId", column: x => x.CityId, principalTable: "Cities", principalColumn: "Id", onDelete: ReferentialAction.Restrict); }); migrationBuilder.CreateIndex( name: "IX_People_CityId", table: "People", column: "CityId"); } protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( name: "People"); migrationBuilder.DropTable( name: "Cities"); } } }
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Vs2015WinFormsEfcSqliteCodeFirst20170304Example.Model; namespace Vs2015WinFormsEfcSqliteCodeFirst20170304Example.Migrations { [DbContext(typeof(Vs2015WinFormsEfcSqliteCodeFirst20170304ExampleContext))] partial class Vs2015WinFormsEfcSqliteCodeFirst20170304ExampleContextModelSnapshot : ModelSnapshot { protected override void BuildModel(ModelBuilder modelBuilder) { modelBuilder .HasAnnotation("ProductVersion", "1.1.0-rtm-22752"); modelBuilder.Entity("Vs2015WinFormsEfcSqliteCodeFirst20170304Example.Model.City", b => { b.Property<int>("Id") .ValueGeneratedOnAdd(); b.Property<string>("Name"); b.HasKey("Id"); // b.HasIndex("Name").IsUnique(); b.ToTable("Cities"); }); modelBuilder.Entity("Vs2015WinFormsEfcSqliteCodeFirst20170304Example.Model.Person", b => { b.Property<int>("Id") .ValueGeneratedOnAdd(); b.Property<int?>("CityId"); b.Property<string>("Name"); b.Property<string>("Surname"); b.HasKey("Id"); b.HasIndex("CityId"); b.ToTable("People"); }); modelBuilder.Entity("Vs2015WinFormsEfcSqliteCodeFirst20170304Example.Model.Person", b => { b.HasOne("Vs2015WinFormsEfcSqliteCodeFirst20170304Example.Model.City", "City") .WithMany("People") .HasForeignKey("CityId"); }); } } }
PM> Update-Database -Context "Vs2015WinFormsEfcSqliteCodeFirst20170304ExampleContext"
private void mainButton_Click(object sender, EventArgs e) { // using (var context = new Vs2015WinFormsEfcSqliteCodeFirst20170304ExampleContext()) { foreach (var person in context.People) context.Remove(person); foreach (var city in context.Cities) context.Remove(city); context.SaveChanges(); } // // , using (var context = new Vs2015WinFormsEfcSqliteCodeFirst20170304ExampleContext()) { var praha = new City { Name = "Praha" }; var london = new City { Name = "London" }; var madrid = new City { Name = "Madrid" }; var jan = new Person { Name = "Jan", City = praha }; var john = new Person { Name = "John", City = london }; var juan = new Person { Name = "Juan", City = madrid }; context.Cities.AddRange(praha, london, madrid); context.People.AddRange(jan, john, juan); context.SaveChanges(); } // , // , // ( ) using (var context = new Vs2015WinFormsEfcSqliteCodeFirst20170304ExampleContext()) { // Include(city => city.People) // context.Cities.Single(city => city.Name == "London"); // , .People null. // production .Single , // var london = context.Cities.Include(city => city.People)(city => city.Name == "London"); var peter = new Person { Name = "Peter", City = london }; var john = london.People.Single(person => person.Name == "John"); john.Surname = "Smith"; context.Add(peter); context.Update(john); context.SaveChanges(); } }
Source: https://habr.com/ru/post/324272/
All Articles