【发布时间】:2014-08-03 00:28:06
【问题描述】:
我创建了一个 SQL Server 视图,它在其他表/实体上执行一些相当复杂的逻辑并公开一系列列。我想在实体框架代码优先方法中访问这个视图,当我看到 this 中的讨论时我刚刚创建了一个“FooView.cs”类并将其添加到我的 DbContext 类中的DbSet<FooView> 属性中。
public class FooView
{
public string PartNumber { get; set; }
public string PartType { get; set; }
}
public class CatalogContext : DbContext
{
public DbSet<FooView> FooView { get; set; }
}
但是当我运行我的代码时,我收到一条错误消息:EntityType 'FooView' has no key defined。定义此 EntityType 的键。
CatalogContext _db = new CatalogContext();
var l = _db.FooViews.Select(_ => _.PartNumber);
但我想要它做的就是从 DB dbo.FooView 访问我的视图
这个问题的更简单的解决方案是创建一个 EDMX 文件并访问视图,但我不想有 2 个 DB 上下文。
如果有人能帮助我,我将不胜感激,非常感谢。
【问题讨论】:
标签: c# entity-framework ef-code-first