.net core 迁移中不会生成继承的子表/字段
我正在尝试使用EntityFrameworkCore3.0建模并生成一个具有继承性的数据库 我跟着导游走 在我的代码中,我有3个类:父类是“源”,继承子类是“发布”和“馈线” 该指南告诉我们,子类中的所有字段都将位于全局表中。但事实并非如此 我的模型如下(一个父类,两个子类) 它生成以下设计器代码(看起来正确-检查源、提要和发布实体).net core 迁移中不会生成继承的子表/字段,.net-core,entity-framework-core,.net Core,Entity Framework Core,我正在尝试使用EntityFrameworkCore3.0建模并生成一个具有继承性的数据库 我跟着导游走 在我的代码中,我有3个类:父类是“源”,继承子类是“发布”和“馈线” 该指南告诉我们,子类中的所有字段都将位于全局表中。但事实并非如此 我的模型如下(一个父类,两个子类) 它生成以下设计器代码(看起来正确-检查源、提要和发布实体) // 使用制度; 使用法律模式; 使用Microsoft.EntityFrameworkCore; 使用Microsoft.EntityFrameworkCore
//
使用制度;
使用法律模式;
使用Microsoft.EntityFrameworkCore;
使用Microsoft.EntityFrameworkCore.Infrastructure;
使用Microsoft.EntityFrameworkCore.Migrations;
使用Microsoft.EntityFrameworkCore.Storage.ValueConversion;
名称空间合法迁移
{
[DbContext(typeof(LegalDbContext))]
[迁移(“20191022181406_版本_0_0”)]
部分类版本\u 0\u 0
{
受保护的覆盖无效BuildTargetModel(ModelBuilder ModelBuilder)
{
#pragma警告禁用612、618
建模者
.HasAnnotation(“产品版本”、“3.0.0”)
.HasAnnotation(“Relational:MaxIdentifierLength”,64);
modelBuilder.Entity(“Legal.Models.Issuer”,b=>
{
b、 财产(“发行人ID”)
.ValueGeneratedOnAdd()
.HasColumnType(“int”);
b、 财产(“说明”)
.HasColumnType(“varchar(64)”)
.HasMaxLength(64);
b、 属性(“LandPageUrl”)
.HasColumnType(“长文本”)
.HasMaxLength(2048年);
b、 HasKey(“发行人ID”);
b、 可转帐(“发行人”);
});
modelBuilder.Entity(“Legal.Models.Source”,b=>
{
b、 属性(“源ID”)
.ValueGeneratedOnAdd()
.HasColumnType(“int”);
b、 财产(“说明”)
.HasColumnType(“长文本”);
b、 属性(“鉴别器”)
.IsRequired()
.HasColumnType(“长文本”);
b、 财产(“发行人ID”)
.HasColumnType(“int”);
b、 属性(“源类型”)
.HasColumnType(“int”);
b、 HasKey(“SourceId”);
b、 HasIndex(“发行人ID”);
b、 ToTable(“来源”);
b、 HasDiscriminator(“鉴别器”)。HasValue(“源”);
});
modelBuilder.Entity(“Legal.Models.Feeder”,b=>
{
b、 HasBaseType(“Legal.Models.Source”);
b、 ToTable(“来源”);
b、 HasDiscriminator().HasValue(“馈线”);
});
modelBuilder.Entity(“Legal.Models.Publication”,b=>
{
b、 HasBaseType(“Legal.Models.Source”);
b、 ToTable(“来源”);
b、 HasDiscriminator().HasValue(“发布”);
});
modelBuilder.Entity(“Legal.Models.Source”,b=>
{
b、 HasOne(“Legal.Models.Issuer”,空)
.有许多(“来源”)
.HasForeignKey(“发行人ID”);
});
#pragma警告恢复612618
}
}
}
但是生成脚本只包含源表,没有任何子字段
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
namespace Legal.Migrations
{
public partial class Version_0_0 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Issuer",
columns: table => new
{
IssuerId = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Description = table.Column<string>(maxLength: 64, nullable: true),
LandPageUrl = table.Column<string>(maxLength: 2048, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Issuer", x => x.IssuerId);
});
migrationBuilder.CreateTable(
name: "Source",
columns: table => new
{
SourceId = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
SourceType = table.Column<int>(nullable: false),
Description = table.Column<string>(nullable: true),
Discriminator = table.Column<string>(nullable: false),
IssuerId = table.Column<int>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Source", x => x.SourceId);
table.ForeignKey(
name: "FK_Source_Issuer_IssuerId",
column: x => x.IssuerId,
principalTable: "Issuer",
principalColumn: "IssuerId",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_Source_IssuerId",
table: "Source",
column: "IssuerId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Source");
migrationBuilder.DropTable(
name: "Issuer");
}
}
}
使用Microsoft.EntityFrameworkCore.Metadata;
使用Microsoft.EntityFrameworkCore.Migrations;
名称空间合法迁移
{
公共部分类版本\u 0\u 0:迁移
{
受保护的覆盖作废(MigrationBuilder MigrationBuilder)
{
migrationBuilder.CreateTable(
名称:“发行人”,
列:表=>new
{
IssuerId=table.Column(可空:false)
.Annotation(“MySql:ValueGenerationStrategy”,MySqlValueGenerationStrategy.IdentityColumn),
Description=table.Column(maxLength:64,null:true),
LandPageUrl=table.Column(最大长度:2048,可为空:true)
},
约束:表=>
{
表.PrimaryKey(“PK_发行人”,x=>x.IssuerId);
});
migrationBuilder.CreateTable(
名称:“来源”,
列:表=>new
{
SourceId=table.Column(可空:false)
.Annotation(“MySql:ValueGenerationStrategy”,MySqlValueGenerationStrategy.IdentityColumn),
SourceType=table.Column(可空:false),
Description=table.Column(可空:true),
鉴别器=表.列(可空:false),
IssuerId=table.Column(可空:true)
},
约束:表=>
{
表.PrimaryKey(“PK_Source”,x=>x.SourceId);
表1.外键(
名称:“FK\u来源\u发行人\u发行人ID”,
列:x=>x.IssuerId,
原则性:“发行人”,
主栏:“发行人ID”,
onDelete:referentialiction.Restrict);
});
migrationBuilder.CreateIndex(
名称:“IX_来源_发行人ID”,
表:“来源”,
列:“发行人ID”);
}
受保护的覆盖无效关闭(MigrationBuilder MigrationBuilder)
{
migrationBuilder.DropTable(
名称:“来源”);
migrationBuilder.DropTable(
名称:“发行人”);
}
}
}
dotnet ef migrations add Version_0_0
// <auto-generated />
using System;
using Legal.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Legal.Migrations
{
[DbContext(typeof(LegalDbContext))]
[Migration("20191022181406_Version_0_0")]
partial class Version_0_0
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("Legal.Models.Issuer", b =>
{
b.Property<int>("IssuerId")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Description")
.HasColumnType("varchar(64)")
.HasMaxLength(64);
b.Property<string>("LandPageUrl")
.HasColumnType("longtext")
.HasMaxLength(2048);
b.HasKey("IssuerId");
b.ToTable("Issuer");
});
modelBuilder.Entity("Legal.Models.Source", b =>
{
b.Property<int>("SourceId")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Description")
.HasColumnType("longtext");
b.Property<string>("Discriminator")
.IsRequired()
.HasColumnType("longtext");
b.Property<int?>("IssuerId")
.HasColumnType("int");
b.Property<int>("SourceType")
.HasColumnType("int");
b.HasKey("SourceId");
b.HasIndex("IssuerId");
b.ToTable("Source");
b.HasDiscriminator<string>("Discriminator").HasValue("Source");
});
modelBuilder.Entity("Legal.Models.Feeder", b =>
{
b.HasBaseType("Legal.Models.Source");
b.ToTable("Source");
b.HasDiscriminator().HasValue("Feeder");
});
modelBuilder.Entity("Legal.Models.Publication", b =>
{
b.HasBaseType("Legal.Models.Source");
b.ToTable("Source");
b.HasDiscriminator().HasValue("Publication");
});
modelBuilder.Entity("Legal.Models.Source", b =>
{
b.HasOne("Legal.Models.Issuer", null)
.WithMany("Sources")
.HasForeignKey("IssuerId");
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
namespace Legal.Migrations
{
public partial class Version_0_0 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Issuer",
columns: table => new
{
IssuerId = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Description = table.Column<string>(maxLength: 64, nullable: true),
LandPageUrl = table.Column<string>(maxLength: 2048, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Issuer", x => x.IssuerId);
});
migrationBuilder.CreateTable(
name: "Source",
columns: table => new
{
SourceId = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
SourceType = table.Column<int>(nullable: false),
Description = table.Column<string>(nullable: true),
Discriminator = table.Column<string>(nullable: false),
IssuerId = table.Column<int>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Source", x => x.SourceId);
table.ForeignKey(
name: "FK_Source_Issuer_IssuerId",
column: x => x.IssuerId,
principalTable: "Issuer",
principalColumn: "IssuerId",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_Source_IssuerId",
table: "Source",
column: "IssuerId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Source");
migrationBuilder.DropTable(
name: "Issuer");
}
}
}
// Define the table based collections
public DbSet<Issuer> Issuers { get; set; }
// public DbSet<Source> Sources { get; set; }
public DbSet<Publication> Publications { get; set; }
public DbSet<Feeder> Feeders { get; set; }
// Force creation of child Sources
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Publication>();
modelBuilder.Entity<Feeder>();
}
public DbSet<Publication> Publications { get; set; }
public DbSet<Feeder> Feeders { get; set; }
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Legal.Models {
public enum SourceType { Publication, Feeder }
// An issuer can have one or more sources. Every source has a type among 3 types
public abstract class Source {
// PK
public int SourceId { get; set; }
// Attributes
public SourceType SourceType { get; set; }
[MaxLength(256)]
public string Description { get; set; }
[MaxLength(1024)]
public string ServiceClass { get; set; }
// Parent/Many to One relation shipp
public int IssuerId { get; set; }
public Issuer Issuer { get; set; }
}
public class Publication : Source {
[MaxLength(13)]
public string ISSN { get; set; }
[MaxLength(2048)]
public string IssueTocUrl { get; set; }
// Generate FK in Issues
public List<PublicationIssue> PublicationIssues { get; set; }
}
public class Feeder : Source {
[MaxLength(2048)]
public string FeederUrl { get; set; }
}
}