1

我有一個簡單的類,它是由實體框架5,類似於生成的Web API 2 XML序列化是忽略MetadataType

public partial class Car 
{ 
    public string Model {get; set;} 
    // other properties follow... 
} 

我創建了一個伴侶類(以保持這些東西被覆蓋)和一個「夥伴級」當我在幫助/阿比運行該應用程序爲我的汽車的幫助頁面保存元數據::

[MetadataType(typeof(CarMetadata))] 
public partial class Car { } 

[DataContract("Automobile")] 
public partial class CarMetadata 
{ 
    [DataMember] 
    public string Model {get; set;} 
} 

/GET-API的汽車模型給我這個錯誤:

An exception has occurred while using the formatter 'XmlMediaTypeFormatter' 
to generate sample for media type 'application/xml'. 
Exception message: One or more errors occurred. 

該kicker是,如果我把EF生成的類DataAnnotations它工作正常。這就像它忽略了好友類...但是JSON格式化程序正在按預期翻譯它。

這給出正確的結果,在EF類,但它不能呆在那裏,或者它覆蓋:

[DataContract("Automobile")] 
public partial class Car 
{ 
    [DataMember] 
    public string Model {get; set;} 
    // other properties follow... 
} 

任何援助將不勝感激。

回答

0

像安東,我試圖重複你的代碼,但它也爲我工作。

我的猜測是你的模型比你在帖子中放置的示例模型更復雜,並且你有引用循環和/或大量的對象嵌套。

如果存在大量嵌套,則嘗試設置config.Formatters.XmlFormatter.MaxDepth = 2。 (如果有效,調整值以符合您的需求)。

如果這些類在對象樹的任意點包含循環引用,則使用[DataContract(IsReference=true)]

如果兩個問題都存在,則同時使用。結合 config.Formatters.XmlFormatter.UseXmlSerializer = true/false確保您嘗試所有選項。

這是我最好的嘗試。希望能幫助到你。

+0

這是最接近實際的問題。引用循環確實是問題。 –

0

也許(我無法在任何地方找到任何信息)XML序列化框架本身並沒有檢測到夥伴類別MetadataType本身。

嘗試在應用程序的init中手動註冊好友類。

這裏的一個例子:

EF實體:

Partial Public Class Clasificacion 

    Public Property ID() As String 
    End Property 

    Private _ID As String 

    Public Property Descripcion() As String 
    End Property 

    Private _Descripcion As String 
End Class 

元數據:

Public Class ClasificationMetadata 

    <StringLength(50, ErrorMessage:="Description too long")> _ 
     Public Property Description() As String 
     Get 
     End Get 
     Set 
     End Set 
    End Property 

End Class 

擴展部分類與元數據:

<MetadataType(GetType(ClasificationMetadata))> _ 
Partial Public Class Clasification 

    Public Function hasDescrition As Boolean 
     Return not String.IsNullOrEmpty(Me.Description) 
End Function 

End Class 

註冊好友類:

Dim descriptionProvider = New AssociatedMetadataTypeTypeDescriptionProvider(GetType(Clasification), GetType(ClasificationMetadata)) 
TypeDescriptor.AddProvider(descriptionProvider, GetType(Clasification)) 
1

老實說,我試圖重複你的代碼,但它適用於我。

所以我生成的模型類汽車

public partial class Car 
    { 
     public int Id { get; set; } 
     public string Model { get; set; } 
    } 

接着我所創建的相同的代碼

[MetadataType(typeof(CarMetadata))] 
public partial class Car { } 


[DataContract()] 
public partial class CarMetadata 
{ 
    [DataMember] 
    public string Model { get; set; } 
} 

接着我創建了一個WAPI控制器

public class EFController : ApiController 
{ 
    // GET api/values 
    public Car Get() 
    { 
     return new Car() 
     { 
      Id = 1, 
      Model = "Hello World!" 
     }; 
    } 
} 

從客戶端予後撥打電話

$.ajax({ 
     url: '/api/EF', 
     type: 'Get', 
     dataType: "xml", 
     success: function (data) { 
       alert(data); 
     }, 
     error: function(XMLHttpRequest, textStatus, errorThrown) { 
       alert("Status: " + textStatus); alert("Error: " + errorThrown); 
     } 
}); 

然後我找回了一個xml對象,其中包含「hello world」和Id(子項)。 對不起,也許我不明白你的問題的性質。請給我解釋一下嗎?否則,它絕對有效!

只是爲了......你使用dataType:「xml」? 並嘗試添加config.Formatters.XmlFormatter.UseXmlSerializer = true; 在WebApiConfig(但它爲我工作在兩種情況下使用和不使用)