我正在使用MVC 5,Microsoft OData 2.1和Entity Framework6。我正在尝试使用使用EntitySetController类编写的OData提要。当我添加服务引用并使用OData元数据时,它可以看到我的资源,但是当我尝试保存它时,我得到了一个错误:
自定义工具“ DataServicesCoreClientGenerator”失败。数据服务客户端代码生成失败:指定的架构无效。错误:(0,0):错误0159:实体'...'未定义键。定义此EntityType的键。
我的OData元数据如下所示:
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="3.0" m:MaxDataServiceVersion="3.0">
<Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="WcfResearch.Entities">
<EntityType Name="vApplication">
<Property Name="ApplicationName" Type="Edm.String"/>
<Property Name="LoweredApplicationName" Type="Edm.String"/>
<Property Name="ApplicationId" Type="Edm.Guid" Nullable="false"/>
<Property Name="Description" Type="Edm.String"/>
</EntityType>
</Schema>
<Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="Default">
<EntityContainer Name="Container" m:IsDefaultEntityContainer="true">
<EntitySet Name="Applications" EntityType="WcfResearch.Entities.vApplication"/>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
我尝试使用以下代码将[Key]属性添加到vApplication类:
[MetadataType(typeof(vApplicationKey))]
public partial class vApplication
{
}
public class vApplicationKey
{
[Key]
public System.Guid ApplicationId { get; set; }
}
但这并不会改变我的元数据,并且错误仍然存在。EDMX模型中的实体密钥设置为ApplicationId。这似乎也不会影响元数据。如何获取元数据以生成具有实体密钥的架构,以便可以在带有服务引用的C#中使用它?
附言:我正在欣赏一个观点。当我直接使用一个表时,它似乎可以工作。那么如何使它与Views一起使用?
您正在使用ODataModelBuilder来构建模型,对吗?如果是,请尝试以下操作:
EntityType _vApplicationKey=oDataModelBuilder.Entity<vApplicationKey>()
_vApplicationKey.HasKey(a => a.ApplicationId);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句