0

这是错误:

无法序列化 System.Collections.Generic.ICollection`1[[baicadicungnamthang.Areas.admin.Models.User, baicadicungnamthang, Version=1.0.0.0, Culture=neutral, PublicKeyToken 类型的成员 baicadicungnamthang.Areas.admin.Models.Role.Users =null]] 因为它是一个接口。

webservice.asmx文件中的getSong(int id)函数出现错误

下面是详细代码:

角色类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace baicadicungnamthang.Areas.admin.Models
{
    public class Role
    {
        [Key]
        public int RoleID { get; set; }
        public string Title { get; set; }

        public virtual ICollection<User> Users { get; set; }
    }
}

用户等级:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using baicadicungnamthang.Areas.admin.Models.AlbumBase;
using baicadicungnamthang.Areas.admin.Models.Social;
using baicadicungnamthang.DAL;
using ICB;

namespace baicadicungnamthang.Areas.admin.Models
{
    public class User : Generic
    {
        [Key]
        public int UserID { get; set; }
        public virtual Role Role { get; set; }
    }
}

歌曲类:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using baicadicungnamthang.Areas.admin.Models.Category;
using baicadicungnamthang.Areas.admin.Models.Person;
using baicadicungnamthang.DAL;

namespace baicadicungnamthang.Areas.admin.Models.SongBase
{
    public class Song : SongBase
    {
        [Key]
        public int SongID { get; set; }
        public virtual ICollection<Composer> Composers { get; set; }
    }
}

网络服务.asmx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using baicadicungnamthang.Areas.admin.Models.SongBase;
using baicadicungnamthang.DAL;

namespace baicadicungnamthang
{
    /// <summary>
    /// Summary description for webservice
    /// </summary>
    [WebService(Namespace = "http://baicadicungnamthang.net/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class webservice : System.Web.Services.WebService
    {
        private baicadicungnamthangContext db = new baicadicungnamthangContext();

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public Song getSong(int id)
        {
            Song song = db.Songs.Find(id);
            return song;
        }
    }
}
4

1 回答 1

0

您不能序列化接口。对这个问题的回答将解释你为什么。看看这个

于 2013-05-01T17:35:58.367 回答