繁体   English   中英

C#WCF-ADO.NET实体模型-DbSet是否为空?

[英]C# WCF - ADO.NET Entity model - DbSet empty?

我在Visual Studio中基于现有数据库创建了一个新的WCF项目。 我做了两次手术。 一项操作将记录(createProfile)写入数据库,而一项检索数据(GetProfiles)。 我的项目包含3个文件:web.config,一个edmx文件和我的svc类。

CreateProfile工作正常,我在SQL中签入并创建了记录。 GetProfiles从不给出响应。 当我调试context.UserProfileSet总是计数0值。

关于出什么问题有什么建议吗?

[DataContract]
public partial class UserProfile
{
    public int Id { get; set; }
    [DataMember]
    public string UserName { get; set; }
}




public class MusicOwnerService : IMusicOwnerService
{
    IEnumerable<UserProfile> GetProfiles()
    {
        using (MusicOwnerDatabaseEntities context = new MusicOwnerDatabaseEntities())
        {
            return context.UserProfileSet.AsEnumerable();
        }
    }


    public void CreateProfile()
    {
        using (MusicOwnerDatabaseEntities context = new MusicOwnerDatabaseEntities())
        {
            context.UserProfileSet.Add(new UserProfile { UserName = "John" });
            context.SaveChanges();
        }
    }
}

据我所知,您不能使用WCF在电线上传递IEnumerable对象(除非您具有某种形式的双工绑定?)。因此,最好转换为列表并返回该列表,如下所示:

List<UserProfile> GetProfiles()
    {
        using (MusicOwnerDatabaseEntities context = new MusicOwnerDatabaseEntities())
        {
            return context.UserProfileSet.ToList();
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM