C# 创建器自定义枚举序列化错误
我试图让breeze与实体框架结合使用我的webapi,但当我试图查询使用自定义枚举的特定实体时,似乎遇到了问题 我正在使用breeze EDMbuilder为我的元数据生成edm模型 我的配置: 以及一个简单的Odatacontroller get函数,该函数返回ApiUserEntities的iqueryable: 这个错误不仅是当我用breeze查询它时出现的,而且是当我从浏览器中访问该方法时出现的 在生成的元数据文件中,apiuserentity如下所示:C# 创建器自定义枚举序列化错误,c#,entity-framework,asp.net-web-api,odata,breeze,C#,Entity Framework,Asp.net Web Api,Odata,Breeze,我试图让breeze与实体框架结合使用我的webapi,但当我试图查询使用自定义枚举的特定实体时,似乎遇到了问题 我正在使用breeze EDMbuilder为我的元数据生成edm模型 我的配置: 以及一个简单的Odatacontroller get函数,该函数返回ApiUserEntities的iqueryable: 这个错误不仅是当我用breeze查询它时出现的,而且是当我从浏览器中访问该方法时出现的 在生成的元数据文件中,apiuserentity如下所示: 我注意到的主要一点是,
我注意到的主要一点是,它为常见类型(如字符串和日期时间)添加了一个Edm前缀。但我的自定义枚举只是它们的完整名称空间。当我将自定义枚举的属性更改为int时,它将返回结果,但我确实希望使用这些枚举,而将它们转换为int并不是一个解决方案
我正在使用它找不到类型,也不知道如何解析它,但这只是使用而已。除此之外,我不知道如何解决这个问题,也不知道我应该从这里走到哪里。在过去的几个小时里,我一直在为这件事头疼,但没有结果 我使用以下类来构建Edm模型:
public class ApiUserEntity // : BaseEntity
{
public int Id { get; set; }
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; }
}
public enum ApiUserRole
{
Admin,
Guest
}
public enum ApiPermission
{
Write,
Read,
WriteRead
}
以下是元数据文档:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="WebApplication1.Models" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="ApiUserEntity">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="Username" Type="Edm.String" />
<Property Name="Password" Type="Edm.String" />
<Property Name="Email" Type="Edm.String" />
<Property Name="Salt" Type="Edm.String" />
<Property Name="Role" Type="WebApplication1.Models.ApiUserRole" Nullable="false" />
<Property Name="Permission" Type="WebApplication1.Models.ApiPermission" Nullable="false" />
</EntityType>
<EnumType Name="ApiUserRole">
<Member Name="Admin" Value="0" />
<Member Name="Guest" Value="1" />
</EnumType>
<EnumType Name="ApiPermission">
<Member Name="Write" Value="0" />
<Member Name="Read" Value="1" />
<Member Name="WriteRead" Value="2" />
</EnumType>
</Schema>
<Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityContainer Name="Container">
<EntitySet Name="ApiUserEntitys" EntityType="WebApplication1.Models.ApiUserEntity" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
希望它能帮助你。谢谢。我遇到了完全相同的问题。你找到解决办法了吗?
public class ApiUserEntity // : BaseEntity
{
public int Id { get; set; }
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; }
}
public enum ApiUserRole
{
Admin,
Guest
}
public enum ApiPermission
{
Write,
Read,
WriteRead
}
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="WebApplication1.Models" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="ApiUserEntity">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="Username" Type="Edm.String" />
<Property Name="Password" Type="Edm.String" />
<Property Name="Email" Type="Edm.String" />
<Property Name="Salt" Type="Edm.String" />
<Property Name="Role" Type="WebApplication1.Models.ApiUserRole" Nullable="false" />
<Property Name="Permission" Type="WebApplication1.Models.ApiPermission" Nullable="false" />
</EntityType>
<EnumType Name="ApiUserRole">
<Member Name="Admin" Value="0" />
<Member Name="Guest" Value="1" />
</EnumType>
<EnumType Name="ApiPermission">
<Member Name="Write" Value="0" />
<Member Name="Read" Value="1" />
<Member Name="WriteRead" Value="2" />
</EnumType>
</Schema>
<Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityContainer Name="Container">
<EntitySet Name="ApiUserEntitys" EntityType="WebApplication1.Models.ApiUserEntity" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
[EnableQuery]
public IQueryable<ApiUserEntity> GetApiUsers()
{
return _userService.GetUsers();
}
{
"@odata.context":"http://localhost:40502/odata/$metadata#ApiUserEntitys","value":[
{
"Id":1,"Username":"UserName #1","Password":"Password #1","Email":"Email #1","Salt":"Salt E1","Role":"Admin","Permission":"WriteRead"
},{
"Id":2,"Username":"UserName #2","Password":"Password #2","Email":"Email #2","Salt":"Salt E2","Role":"Admin","Permission":"WriteRead"
},{
"Id":3,"Username":"UserName #3","Password":"Password #3","Email":"Email #3","Salt":"Salt E3","Role":"Admin","Permission":"WriteRead"
},{
"Id":4,"Username":"UserName #4","Password":"Password #4","Email":"Email #4","Salt":"Salt E4","Role":"Admin","Permission":"WriteRead"
},{
"Id":5,"Username":"UserName #5","Password":"Password #5","Email":"Email #5","Salt":"Salt E5","Role":"Admin","Permission":"WriteRead"
}
]
}