我已经定义了这些类:
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
答案 0 :(得分:0)
您可以编写代码来构建这样的模型 &#34; ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet(&#34;报告&#34;);&#34; 它将自动添加引用的复杂类型&#34; ReportRow&#34;并自动将其标记为开放式。
如果你想像示例那样继续使用ODataModelBuilder,那么你需要添加reportType.HasKey(c =&gt; c.ID);并删除reportType.Property(c =&gt; c.ID);在定义实体类型时,必须定义一个键。
您可以参考http://odata.github.io/WebApi/#02-01-model-builder-abstract了解如何使用不同的模型构建器来构建模型。