2014-12-07 59 views
1

我想讓我的webapi和實體框架一起工作,但當我嘗試查詢使用自定義枚舉的某個實體時,似乎卡住了。Breeze EdmBuilder自定義枚舉序列化錯誤

我使用微風EDMbuilder來爲我的元數據生成edm模型。

我的配置:

config.Routes.MapODataServiceRoute(
    routeName: "odata", 
    routePrefix: "odata", 
    model: EdmBuilder.GetEdm<Base.DAL.Entities.DbContextFixForEdm>(), 
    batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer) 
); 

的元數據生成的,如果我查詢的OData/$元,我看到我所有的實體及其屬性。

現在我面臨的問題如下。

我有一個名爲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獲取函數返回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時,它會給我返回的結果,但我真的想要使用這些枚舉並將它們轉換爲整數將不是解決方案。

我正在尋找它不能找到類型,並不知道如何解析它,但這只是geussing。除此之外,我不知道如何解決它或我應該從這裏走。在過去的幾個小時裏,我一直沒有結果地抨擊我的頭腦。

+0

我遇到了完全相同的問題。你有沒有找到解決方案? – 2015-02-19 04:29:30

回答

1

我用下面的類來建立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> 

自定義枚舉類型及其完整的命名空間是正確的。我認爲你的問題是你的方法定義。請嘗試修改您的方法:

[EnableQuery] 
public IQueryable<ApiUserEntity> GetApiUsers() 
{ 
    return _userService.GetUsers(); 
} 

也就是說,不寫EnableQueryAttributeODataQueryOptions<ApiUserEntity> opts在同一時間的方法。

改造後,這裏是我的樣品測試:

GET〜/的OData/ApiUserEntitys

{ 
    "@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" 
    } 
    ] 
} 

希望它可以幫助你。謝謝。