EntityFramework:使用视图扩展表 [英] EntityFramework: using a view to extend a table
本文介绍了EntityFramework:使用视图扩展表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用视图向这样的表中添加信息
I'd like to use a view to add information to a table like this
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(核心)没有任何内容,原因是:
But EntityFramework (core) isn't having any of it because:
实体类型"PocoView"不能映射到表,因为它是从"PocoTable"派生的.只有基本实体类型可以映射到表.
The entity type 'PocoView' cannot be mapped to a table because it is derived from 'PocoTable'. Only base entity types can be mapped to a table.
可以使它起作用吗?还是我可以通过其他方式做到这一点?
Can this be made to work? Or can I do this some other way?
推荐答案
您可以使用PocoView 的数据库继承),例如
You can use HasBaseType(Type) method and pass null
to let EF Core not treat PocoTable
as base entity (part of database inheritance) of PocoView
, e.g.
modelBuilder.Entity<PocoView>()
.HasBaseType((Type)null) // <--
.ToView("PocoView");
这篇关于EntityFramework:使用视图扩展表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文