使用EF6存储功能的实体框架代码优先,我可以返回一个自定义类型? [英] Using EF6 Store Functions for Entity Framework code-first, can I return a custom type?
问题描述
我有一个实体类:
public class SomeClass {
public int Id { get; set; }
public int Value { get; set; }
public string Name { get; set; }
}
使用的 EF6商店由Moozzyk实体框架代码优先功能,我看到映射到实体类型的函数的示例代码。
using the EF6 Store Functions for Entity Framework code-first by Moozzyk, I see example code that maps a function to an entity type.
然而,使用尚未被映射为实体类型时,我收到一个异常说类型不是有效的实体类型
However, when using a type that isn't already mapped as an entity, I receive an exception saying the type is not a valid entity type.
例如:
[DbFunction("MyContext", "GetValueSum")]
public IQueryable<SomeClassSummary> GetValueSum()
{
return ((IObjectContextAdapter)this).ObjectContext
.CreateQuery<SomeClassSummary>(string.Format("[{0}].{1}", GetType().Name,
"[GetValueSum]()"));
}
我怎么能输出函数的调用特定类型?
How can I output the call of that function to a specific type?
推荐答案
命名一样的功能要返回必须具有列的类型。例如,如果该函数返回列:
The type to be returned must have columns named the same as the function. For example, if the function returns columns:
Name nvarchar
Sum int
然后SomeClassSummary应该是:
then SomeClassSummary should be:
public class SomeClassSummary {
public string Name { get; set; }
public int Sum { get; set; }
}
然后在上下文中添加类作为一个复杂类型:
Then in the context, add the class as a complex type:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.ComplexType<SomeClassSummary>();
modelBuilder.Conventions.Add(new FunctionsConvention<MyContext>("dbo"));
}
这篇关于使用EF6存储功能的实体框架代码优先,我可以返回一个自定义类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!