【问题标题】:Serialize genereted classes from ADO.NET entity model从 ADO.NET 实体模型序列化生成的类
【发布时间】: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


【解决方案1】:

尽管您可以使用多种方法(如 cmets 中所述)(禁用代理生成)来实现您想要的。

如果您的架构允许,我建议您将DTOs(数据传输对象)与automapper 等映射工具一起使用。

【讨论】:

    【解决方案2】:

    我是这样解决的:

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

    现在可以正常使用了。

    【讨论】:

    • 这不是推荐的方法。尽管从技术上讲这可能可行,但在通信层上公开持久性对象通常会导致其他问题(性能、安全性)。请改用@Luis 的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 2011-12-07
    相关资源
    最近更新 更多