0

序列化genereted类我有以下表:从ADO.NET实体模型

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,文化=中性公钥= b77a5c561934e089

消息:

尝试序列化参数 http://tempuri.org/:list_moviesResult时发生错误。该消息的InnerException是 “类型 ‘System.Data.Entity.DynamicProxies.Movie_46EC56490AEEEA50CA29379C6E09B01C345ECBB1273756AE368BA72AA30754B5’ 数据合同名称 ‘Movie_46EC56490AEEEA50CA29379C6E09B01C345ECBB1273756AE368BA72AA30754B5:预计不会http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies’ 。考虑使用DataContractResolver或将任何未知的 类型静态添加到已知类型的列表中 - 例如,使用KnownTypeAttribute属性的 或将其添加到传递给DataContractSerializer的已知类型的 列表中。有关更多详细信息,请参阅 InnerException。

让我知道是否需要更多细节。

+0

在实体模型中禁用代理生成。 –

回答

0

这是我如何解决它:

我删除生成的类,并下载一个新的代码生成项模板:EF 6.x的EntityObject生成器。 (我使用DBContext生成器)。

现在它工作正常。

+1

这不是推荐的方法。尽管技术上这可能有效,但在通信层上公开持久对象通常会导致其他问题(性能,安全性)。请按照@Luis建议。 –

1

尽管您可以使用几种方法来实现您想要的功能,如一个注释(禁用代理生成)中所述。

如果你的架构允许它,我会推荐使用DTOs(数据传输对象)以及像automapper这样的映射工具。