Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NETCore3.1中的OData配置_C#_Asp.net Core_Odata_Asp.net Core Webapi - Fatal编程技术网

C# .NETCore3.1中的OData配置

C# .NETCore3.1中的OData配置,c#,asp.net-core,odata,asp.net-core-webapi,C#,Asp.net Core,Odata,Asp.net Core Webapi,我已经在我的应用程序中配置了OData,并且能够在WeatherForecast模型上执行基本操作。 然而,当我试图查询地址属性时,在#1和#2方法中会出现以下异常,在#3方法中,我能够查询地址属性 异常消息:“URI中指定的查询无效。不允许在“地址”选择中嵌套查询选项。” Edm模型方法和非Edm模型方法有什么区别?必须在模型上具有id属性才能在Edm中注册。此外,当模型没有id属性时,它会给出错误 $exception{“实体集'WeatherForecast'基于未定义键的类型'Weath

我已经在我的应用程序中配置了OData,并且能够在WeatherForecast模型上执行基本操作。 然而,当我试图查询地址属性时,在#1和#2方法中会出现以下异常,在#3方法中,我能够查询地址属性

异常消息:“URI中指定的查询无效。不允许在“地址”选择中嵌套查询选项。”

Edm模型方法和非Edm模型方法有什么区别?必须在模型上具有id属性才能在Edm中注册。此外,当模型没有id属性时,它会给出错误

$exception{“实体集'WeatherForecast'基于未定义键的类型'WeatherAPI.WeatherForecast'。System.InvalidoOperationException

接近

#1
    endpoints.EnableDependencyInjection();
    endpoints.Select().Filter().OrderBy().Count().MaxTop(null);
    endpoints.MapODataRoute("odata", null, GetEdmModel());

#2
    var builder = new ODataConventionModelBuilder();

    endpoints.EnableDependencyInjection();
    endpoints.Select().Filter().OrderBy().Count().MaxTop(null);
    endpoints.MapODataRoute("odata", null, builder.GetEdmModel());

#3
    endpoints.Select().Filter().OrderBy().Count().MaxTop(null);
    endpoints.EnableDependencyInjection(b =>
    {
        b.AddService(Microsoft.OData.ServiceLifetime.Singleton, typeof(IEdmModel), sp => GetEdmModel());
    });
    public class WeatherForecast
{
    public Guid Id { get; set; }

    public DateTime Date { get; set; }

    public int TemperatureC { get; set; }

    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

    public string Summary { get; set; }

    public Address Addresses { get; set; }
}

public class Address
{
    public string StreetId { get; set; }

    public string StreetName { get; set; }
}
GetedModel

public static IEdmModel GetEdmModel()
    {
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<WeatherForecast>("WeatherForecast");

        return builder.GetEdmModel();
    }

DBContext文件的外观如何

实体框架需要知道哪一个是关键,我猜这就是这里缺少的部分。虽然默认情况下EF将选择属性Id,但由于它是Guid,因此指定它会更安全

尝试更改WeatherForecast模型,例如:

public class WeatherForecast
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    public DateTime Date { get; set; }

    public int TemperatureC { get; set; }

    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

    public string Summary { get; set; }

    public Address Addresses { get; set; }
}

如果您想了解更多,请查看以下答案:
-,

-

@Piyo,谢谢你的更新。Edm和非Edm方法的区别是什么?