视图和组合键 [英] Views and composite key
问题描述
我有一个无法修改数据库的场景
有一个视图包含三个不同的表ID和其他字段。
I have a scenario where the db cannot be modified at all
There is a View that contains three different table ids and other fields.
为了从视图中读取数据,我必须声明一个PK(因为它是readonly,这不应该是一个要求)。
我试图以这种方式建模,但是CTP4 fais,错误"键表达式无效。"
In order to read the data from the view, I have to declare a PK (as it is readonly, this should not be a requirement).
I tried to model the key this way, but CTP4 fais with the error "Key expression is not valid."
modelBuilder.Entity<Stocked>().HasKey(
p => new
{
ArticleId = p.Article.Id,
WarehouseId = p.Warehouse.Id,
BatchId = p.Batch.Id,
});
问题是我的实体不包含"ID"作为int但是对另一个实体的引用所以我没有要放入lambda的属性。
The problem is that my entity does not contain the "ID" as int but the reference to another entity so I have no properties to put in the lambda.
问题:
1.是否有另一种方式来声明PK(或完全避免它)?
2.对于下一个CTP / beta / RTM,我坚信不应该只读取(仅限AKA选择)数据的PK。我错过了什么吗?
Questions:
1. Is there an alternative way to declare the PK (or to avoid it at all)?
2. For the next CTP/beta/RTM I strongly believe there should not be the requirement for PKs for readonly (AKA select-only) data. Am I missing something?
谢谢
推荐答案
嗨Raffaele,
Hi Raffaele,
Code First目前仅限支持映射到读/写表&观点。 EF一般支持映射到只读查看数据,但在下一版本的Code First中不支持,我们计划在将来支持这一点。
Code First currently only supports mapping to read/write tables & views. EF in general does support mapping to readonly view data but this won't be supported in Code First in the next release, we do plan to support this in the future.
最好的选择是是使用底层上下文对视图运行查询:
The best option would be to use the underlying context to run a query against the view:
public class MyContext : DbContext
{
...
public IEnumerable<Stocked> GetStocked()
{
return this.ObjectContext.ExecuteStoreQuery<Stocked>("SELECT * FROM dbo.StockedView");
}
}
这篇关于视图和组合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!