【发布时间】:2014-03-30 13:15:21
【问题描述】:
我有下表:
create table Movie
(
id integer primary key identity(1,1),
title varchar(40),
synopsis varchar(200),
movieLength integer,
imageSmall varbinary(max),
imageLarge varbinary(max),
inputDate date,
);
使用 ADO.NET 实体模型,我生成了 C# 类。
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace CinemaService
{
using System;
using System.Collections.Generic;
public partial class Movie
{
public Movie()
{
this.Show = new HashSet<Show>();
}
public int id { get; set; }
public string title { get; set; }
public string synopsis { get; set; }
public Nullable<int> movieLength { get; set; }
public byte[] imageSmall { get; set; }
public byte[] imageLarge { get; set; }
public Nullable<System.DateTime> inputDate { get; set; }
public virtual ICollection<Show> Show { get; set; }
}
}
我有以下合同:
namespace CinemaService
{
[ServiceContract]
public interface ICinemaService
{
[OperationContract]
Movie[] list_movies();
}
}
和服务:
namespace CinemaService
{
[ServiceBehavior]
public class CinemaService : ICinemaService
{
private CinemaEntities _entities;
public CinemaService()
{
_entities = new CinemaEntities();
}
public Movie[] list_movies()
{
return _entities.Movie.ToArray();
}
}
}
服务托管在 IIS 上。
当表为空时,我可以调用并获取结果(空结果),但是当只有一行时,我得到一个异常。
我在 web.config 中启用了跟踪,我认为问题在于生成的类不可序列化。
使用跟踪查看器,我发现了以下内容:
异常类型:
System.ServiceModel.CommunicationException,System.ServiceModel, 版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089
消息:
尝试序列化参数时出错 http://tempuri.org/:list_moviesResult。 InnerException 消息是 '类型 'System.Data.Entity.DynamicProxies.Movie_46EC56490AEEEA50CA29379C6E09B01C345ECBB1273756AE368BA72AA30754B5' 带有数据合同名称 '电影_46EC56490AEEEA50CA29379C6E09B01C345ECBB1273756AE368BA72AA30754B5:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' 预计不会。考虑使用 DataContractResolver 或添加任何 已知类型列表中静态未知的类型 - 例如, 通过使用 KnownTypeAttribute 属性或将它们添加到 传递给 DataContractSerializer 的已知类型列表。请参见 InnerException 了解更多详情。
如果需要更多详细信息,请告诉我。
【问题讨论】:
-
在您的实体模型中禁用代理生成。
标签: c# asp.net entity-framework serialization ado.net-entity-data-model