[英]EF Include navigation property with specific Map
I have this two classes on EF NET Core wich represent database objects: 我在EF NET Core上有这两个类,它们分别代表数据库对象:
public partial class Clientes
{
public int IdCliente { get; set; }
public TiposIva CondicionIva { get; set; }
}
public partial class TiposIva
{
public int IdCondicionIva { get; set; }
public string Descripcion { get; set; }
public string Letra { get; set; }
public string Fiscal { get; set; }
public Enumerador ToEnumerador() {
return new Enumerador { ID = this.IdCondicionIva, Valor = this.Descripcion };
}
}
Now I'm trying to write a query that return a Clientes object, and include TiposIva property (navigation), but not all of them, Only the ones represented on ToEnumerador Method. 现在,我正在尝试编写一个查询,该查询返回一个Clientes对象,并包括TiposIva属性(导航),但不是全部,仅包含ToEnumerador方法上表示的那些。
In fact I need a Clientes Object with a Enumerador Property filled with related TiposIva data 实际上,我需要一个带有Enumerador属性的Clientes对象,其中填充了相关的TiposIva数据
I try this: 我尝试这样:
return this.RepositoryContext.Clientes
.Include(c => c.CondicionIva.ToEnumerador())
.FirstOrDefault();
But I get this error: 但是我得到这个错误:
System.InvalidOperationException HResult=0x80131509 Mensaje = The Include property lambda expression 'c => c.CondicionIva.ToEnumerador()' is invalid.
System.InvalidOperationException HResult = 0x80131509 Mensaje =包含属性lambda表达式'c => c.CondicionIva.ToEnumerador()'无效。 The expression should represent a property access: 't => t.MyProperty'.
该表达式应表示属性访问:“ t => t.MyProperty”。 To target navigations declared on derived types, specify an explicitly typed lambda parameter of the target type, Eg '(Derived d) => d.MyProperty'.
要定位在派生类型上声明的导航,请指定目标类型的显式输入的lambda参数,例如'((派生d)=> d.MyProperty')。 For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393 .
有关包括相关数据的更多信息,请参见http://go.microsoft.com/fwlink/?LinkID=746393 。 Origen = Microsoft.EntityFrameworkCore Seguimiento de la pila: at Microsoft.EntityFrameworkCore.Query.ResultOperators.Internal.IncludeExpressionNode.CreateResultOperator(ClauseGenerationContext clauseGenerationContext) at Remotion.Linq.Parsing.Structure.IntermediateModel.ResultOperatorExpressionNodeBase.ApplyNodeSpecificSemantics(QueryModel queryModel, ClauseGenerationContext clauseGenerationContext) at Remotion.Linq.Parsing.Structure.IntermediateModel.MethodCallExpressionNodeBase.Apply(QueryModel queryModel, ClauseGenerationContext clauseGenerationContext) at Remotion.Linq.Parsing.Structure.QueryParser.ApplyAllNodes(IExpressionNode node, ClauseGenerationContext clauseGenerationContext) at Remotion.Linq.Parsing.Structure.QueryParser.ApplyAllNodes(IExpressionNode node, ClauseGenerationContext clauseGenerationContext) at Remotion.Linq.Parsing.Structure.QueryParser.GetParsedQuery(Expression expressionTreeRoot) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[
Origen = Microsoft.EntityFrameworkCore参考:在Microsoft.EntityFrameworkCore.Query.ResultOperators.Internal.IncludeExpressionNode.CreateResultOperator(ClauseGenerationContext子句GenerationContext)在Remotion.Linq.Parsing.Structure.IntermediateModel.ResultOperatorExpressionNodeBase.ApplyNodeSpecrationModel Remotion.Linq.Parsing.Response.Linq.Parsering.Remote.Linq.Parsing.Structure.IntermediateModel.MethodCallExpressionNodeBase.Apply(Reference.Linq.Parsing.Structure.QueryParser.ApplyAllNodes(IExpressionNode节点,ClauseGenerationContext子句GenerationContext) Remotion.Linq.Parsing.Structure.QueryParser.GetParsedQuery(Expression expressionTreeRoot)处的ApplyAllNodes(IExpressionNode节点,ClauseGenerationContext子句GenerationContext)在Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore [ TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger
1 logger, Type contextType) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_0
1.b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable
1 source) at OhmioRepositorios.ClientesRepository.Cargar(Int32 IdCliente) in C:\\Users\\Eric\\Documents\\Fuentes Ohmio WEB\\Server EF\\OhmioRepositorios\\ClientesRepository.cs:line 40 at OhmioServicios.Servicios.Clientes_svc.Cargar(Int32 id) in C:\\Users\\Eric\\Documents\\Fuentes Ohmio WEB\\Server EF\\OhmioServicios\\Servicios\\Clientes_svc.cs:line 50 at OhmioWEBAPINetCore.Controllers.ClientesController.GetCliente(Int32 idCliente) in C:\\Users\\Eric\\Documents\\Fuentes Ohmio WEB\\Server EF\\OhmioWEBAPINeTResult](
1 logger, Type contextType) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_0
1.b__0()处的表达式查询,IQueryModelGenerator queryModelGenerator,IDatabase数据库,IDiagnosticsLogger1 logger, Type contextType) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_0
1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable
[TFunc](对象cacheKey,Func1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable
OhmioRepositorios.ClientesRepository的[TFResult](1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable
C:\\ Users \\ Eric \\ Documents \\ Fuentes Ohmio WEB \\ Server EF \\ OhmioRepositorios \\ ClientesRepository.cs中的.Cargar(Int32 IdCliente):OhmioServicios.Servicios.Clientes_svc.Cargar(Int32 id)中的第40行\\ Documents \\ Fuentes Ohmio WEB \\ Server EF \\ OhmioServicios \\ Servicios \\ Clientes_svc.cs:OmioWEBAPINetCore.Controllers.ClientesController.GetCliente(Int32 idCliente)的第50行位于C:\\ Users \\ Eric \\ Documents \\ Fuentes Ohmio WEB \\ WEBA \\服务器EF \\ Ohm tCore\\Controllers\\ClientesController.cs:line 38 at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()tCore \\ Controllers \\ ClientesController.cs:Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute的第38行,Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()上的(Object target,Object []参数)
It will not work that way. 这样就行不通了。 "Include" you can only use navigation properties.
“包含”您只能使用导航属性。 You don't need call .ToEnumerador() method inside "Include", use only TiposIva.
您不需要在“包含”内调用.ToEnumerador()方法,只需使用TiposIva。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.