【发布时间】:2021-10-25 17:11:22
【问题描述】:
我想使用视图将信息添加到这样的表中
public class PocoTable
{
public int Id { get; set; }
}
public partial class ImportStatingRecordError : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"
CREATE VIEW PocoView
AS
SELECT Id, ISNULL(l.InterestingValue, '?') AS InterestingValue
FROM PocoTable t
LEFT JOIN OtherTable o ON t.Id = o.PocoTableId
");
}
}
public class PocoView : PocoTable
{
public string InterestingValue { get; set; }
}
public class ApplicationDbContext : DbContext
{
public DbSet<PocoTable> PocoTables { get; set; }
public DbSet<PocoView> PocoViews { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<PocoView>().ToView("PocoView").HasKey(z => new z.Id);
}
}
但 EntityFramework(核心)没有它,因为:
实体类型“PocoView”无法映射到表,因为它派生自“PocoTable”。只有基本实体类型可以映射到表。
这可以工作吗?或者我可以通过其他方式做到这一点吗?
【问题讨论】:
-
你可以使用一个接口来定义结构和实现两个类的ist。然后你可以映射 View-class 并且在使用 Interface 时可以 Target 两者。
标签: entity-framework entity-framework-core