【发布时间】:2015-01-04 15:56:36
【问题描述】:
我正在尝试将我的 webapi 与实体框架结合使用变得轻而易举,但是当我尝试查询使用自定义枚举的某个实体时似乎卡住了。
我正在使用微风 EDMbuilder 为我的元数据生成 edm 模型。
我的配置:
config.Routes.MapODataServiceRoute(
routeName: "odata",
routePrefix: "odata",
model: EdmBuilder.GetEdm<Base.DAL.Entities.DbContextFixForEdm>(),
batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer)
);
生成元数据,如果我查询 odata/$metadata,我会看到我的所有实体及其属性。
现在我面临的问题如下。
我有一个名为 ApiUserEntity 的非常基本的实体,它具有以下属性:
public class ApiUserEntity : BaseEntity
{
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Salt { get; set; }
public ApiUserRole Role { get; set; }
public ApiPermission Permission { get; set; }
}
还有一个简单的 Odatacontroller get 函数,它返回 ApiUserEntities 的 iqueryable:
// GET: odata/ApiUsers
[EnableQuery]
public IQueryable<ApiUserEntity> GetApiUsers(ODataQueryOptions<ApiUserEntity> opts)
{
return _userService.GetUsers();
}
但是,每当我查询此方法时,我总是会返回以下错误:
'Base.DAL.Entities.ApiUserRole' cannot be serialized using the ODataMediaTypeFormatter.
这个错误不仅仅是当我用微风查询它时,而且当我从我的浏览器访问该方法时。
在生成的元数据文件中,apiuserentity 如下所示:
<EntityType xmlns:p5="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" Name="ApiUserEntity" p5:ClrType="Base.DAL.Entities.ApiUserEntity, Base.DAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property xmlns:p7="http://schemas.microsoft.com/ado/2009/02/edm/annotation" Name="Id" Type="Edm.Int32" Nullable="false" p7:StoreGeneratedPattern="Identity"/>
<Property Name="Username" Type="Edm.String" Nullable="false" MaxLength="255" FixedLength="false" Unicode="true"/>
<Property Name="Password" Type="Edm.String" Nullable="false" MaxLength="300" FixedLength="false" Unicode="true"/>
<Property Name="Email" Type="Edm.String" Nullable="false" MaxLength="255" FixedLength="false" Unicode="true"/>
<Property Name="Salt" Type="Edm.String" MaxLength="255" FixedLength="false" Unicode="true"/>
<Property Name="Role" Type="Base.DAL.Entities.ApiUserRole" Nullable="false"/>
<Property Name="Permission" Type="Base.DAL.Entities.ApiPermission" Nullable="false"/>
<Property Name="CreatedAt" Type="Edm.DateTime"/>
<NavigationProperty Name="Domains" Relationship="Base.DAL.Entities.DomainEntity_Users" ToRole="DomainEntity_Users_Source" FromRole="DomainEntity_Users_Target"/>
</EntityType>
我注意到的主要事情是它为字符串和日期时间等常见类型添加了 Edm 前缀。但是我的自定义枚举只是它们的完整命名空间。当我将自定义枚举的属性更改为 int 时,它会给我返回结果,但我真的很想使用这些枚举并将它们转换为 int 不是解决方案。
我猜它找不到类型并且不知道如何解析它,但这只是猜猜。除此之外,我不知道如何解决它或我应该从这里去哪里。在过去的几个小时里,我一直在努力解决这个问题,但没有结果。
【问题讨论】:
-
我遇到了完全相同的问题。你有没有找到解决办法?
标签: c# entity-framework asp.net-web-api odata breeze