[英]Exception using dynamic properties in OData service
我已经定义了这些类:
public class Report
{
[Key]
public long ID { get; set; }
public List<ReportRow> Rows { get; set; }
}
public class ReportRow
{
public IDictionary<string, object> Values { get; set; }
}
我已经像这样配置了网站;
var rowType = builder.ComplexType<ReportRow>();
rowType.HasDynamicProperties(c => c.Values);
var reportType = builder.EntityType<Report>();
reportType.Property(c => c.ID);
reportType.CollectionProperty(c => c.Rows);
builder.EntitySet<Report>("Reports");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: builder.GetEdmModel()
);
但是当我调用上面的config.MapODataServiceRoute
时,我只是得到了这个异常;
An exception of type 'System.ArgumentException' occurred in System.Web.OData.dll but was not handled in user code
Additional information: Found more than one dynamic property container in type 'ReportRow'. Each open type must have at most one dynamic property container.
如果我在上面的示例中从WebApiConfig中删除前两行,我没有得到相同的异常。 我按照此页面上的示例代码: http : //www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/use-open-types-in-odata -v4
您可以编写代码来构建模型,如“ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet(”Reports“);” 它将自动添加引用的复杂类型“ReportRow”并自动将其标记为打开类型。
如果你想像示例那样继续使用ODataModelBuilder,那么你需要添加reportType.HasKey(c => c.ID); 并删除reportType.Property(c => c.ID); 在定义实体类型时,必须定义一个键。
关于如何使用不同的模型构建器来构建模型,可以参考http://odata.github.io/WebApi/#02-01-model-builder-abstract 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.