2013-07-08 54 views
1

我目前正在檢查已有的Web Api項目上OData的可能性。使用代碼首先,我從頭開始創建所有模型,以便我可以絕對控制它們。但是,即使我已經在代碼中添加了導航屬性,但是通過OData提供的元數據鏈接檢出它們時,它們中的一些還缺少。缺少現有的導航屬性

例如,我有一個用戶類,它從一個Person類繼承而來:當我做這個GET請求

[DataContract] 
[KnownType(typeof(User))] 
public abstract class Person 
{ 
    [DataMember] 
    public int ID { get; set; } 
    [DataMember] 
    public string Name { get; set; } 
    [DataMember] 
    public Nullable<int> CountryID { get; set; } 
    [DataMember] 
    public Nullable<int> RelationID { get; set; } 

    public virtual Country Country { get; set; } 
    public virtual Relation Relation { get; set; } 
} 

現在,使用/User(1284)/Relation(或/Country對這個問題)我得到完全相同我想要的關係或鄉村課程。但問題是,我不能撥打/Relation(16)/Country,因爲這種關聯不存在。

[DataContract] 
public class Relation 
{ 
    [DataMember] 
    public int ID { get; set; } 
    [DataMember] 
    public string Number { get; set; } 
    [DataMember] 
    public string Serial { get; set; } 
    [DataMember] 
    public Nullable<int> CountryID { get; set; } 

    public virtual Country Country { get; set; } 
    public virtual List<User> Users { get; set; } 
} 

但是正如您所看到的,在我的關係類中,肯定存在這樣的導航屬性。此外,當你看的DbContext類:

public DbSet<Relation> Relations { get; set; } 
    public DbSet<Country> Countries { get; set; } 
    public DbSet<User> Users { get; set; } 

和WebApiConfig

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder(); 
    modelBuilder.EntitySet<Relation>("Relations"); 
    modelBuilder.EntitySet<Country>("Countries"); 
    modelBuilder.EntitySet<User>("Users"); 

你可以看到,實際上關係和用戶幾乎是相同的導航性能方面,至少在國家,他們是相同的。

只是,正如我所說,當我看着通過的OData本身提供的元數據:

<Association Name="TestProject_Models_User_Country_TestProject_Models_Country_CountryPartner"> 
    <End Type="TestProject.Models.Country" Role="Country" Multiplicity="0..1" /> 
    <End Type="TestProject.Models.User" Role="CountryPartner" Multiplicity="0..1" /> 
    </Association> 
    <Association Name="TestProject_Models_User_Relation_TestProject_Models_Relation_RelationPartner"> 
    <End Type="TestProject.Models.Relation" Role="Relation" Multiplicity="0..1" /> 
    <End Type="TestProject.Models.User" Role="RelationPartner" Multiplicity="0..1" /> 
    </Association> 

你可以看到,用戶 - >國家和用戶>的關係存在,但相對於國家的協會和用戶丟失。但是,在數據庫中,這些關係確實存在並且外鍵已經存在。當我在NuGet控制檯中運行更新數據庫時,我也收到通知,說明沒有代碼更新要完成。

我已經放棄了我的整個數據庫,讓CodeFirst重新創建了一切;更新到OData的最新穩定版本(從4.0.0到4.0.30506),但唉,沒有任何工作。

有沒有人有任何線索讓我跟隨?提前致謝!

+0

在$ metadata文檔中,Relation類型定義是否具有引用「TestProject_Models_User_Relation_TestProject_Models_Relation_RelationPartner」關聯的導航屬性?我假設用戶類型定義確實有這樣的導航屬性。 –

回答

2

當您的課程標記爲[DataContract]時,模型構建器僅挑選標記爲[DataMember]的公共屬性。因此,我不確定模型構建者如何計算出User上的導航屬性CountryRelation

當然,您可以明確告訴模型構建器任何無法推斷的導航屬性。示例代碼,

var relations = modelBuilder.EntitySet<Relation>("Relations"); 
var countries = modelBuilder.EntitySet<Country>("Countries"); 
var users = modelBuilder.EntitySet<User>("Users"); 
users.HasRequiredBinding(u => u.Country, countries); 
users.HasRequiredBinding(u => u.Relation, relations); 
relations.HasRequiredBinding(r => r.Country, countries); 
relations.HasManyBinding(r => r.Users, users); 

其他方法是在導航屬性上添加DataMember屬性以及讓模型構建器自動對它們進行定位。

+0

你的第一個建議是那個!我的想法是,在導航屬性中添加'[DataMember]'並不重要,因爲我將它看作不屬於某個屬性(這當然是愚蠢的)。也因爲用戶沒有這些屬性,我認爲將它們添加到關係並不重要。但它確實有幫助,非常感謝! – Kazu