Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebService(asmx)使用相关对象序列化问题_C#_.net_Asmx - Fatal编程技术网

C# WebService(asmx)使用相关对象序列化问题

C# WebService(asmx)使用相关对象序列化问题,c#,.net,asmx,C#,.net,Asmx,我将EF codefirst用于我的Web服务项目中的现有数据库。我有一个用户对象 public partial class User { public string Id { get; set; } public string Email { get; set; } public bool EmailConfirmed { get; set; } public string PasswordHash { get; set; } public st

我将EF codefirst用于我的Web服务项目中的现有数据库。我有一个用户对象

public partial class User
{
    public string Id { get; set; }

    public string Email { get; set; }

    public bool EmailConfirmed { get; set; }

    public string PasswordHash { get; set; }

    public string SecurityStamp { get; set; }

    public string PhoneNumber { get; set; }

    public bool PhoneNumberConfirmed { get; set; }

    public bool TwoFactorEnabled { get; set; }

    public DateTime? LockoutEndDateUtc { get; set; }

    public bool LockoutEnabled { get; set; }

    public int AccessFailedCount { get; set; }

    public string UserName { get; set; }

    public string Name { get; set; }

    public string Surname { get; set; }

    public DateTime? BirthDay { get; set; }

    public int? BirthPlace { get; set; }

    public string Discriminator { get; set; }

    public int? TeamId { get; set; }

    public int? AvatarId { get; set; }

    public DateTime? RegisterationDate { get; set; }

    public DateTime? CodeSendDate { get; set; }

    public string ActivationCode { get; set; }

    public string PasswordResetToken { get; set; }

    public string FacebookAvatar { get; set; }

    public string FacebookId { get; set; }

    public bool? UseFacebookAvatar { get; set; }

    public string Address { get; set; }

    public string IpAddress { get; set; }

    public bool? SmsCheck { get; set; }

    [XmlIgnore]
    public virtual Avatar Avatar { get; set; }

    [XmlIgnore]
    public virtual ICollection<CouponApplicationUser> CouponApplicationUsers { get; set; }
    [XmlIgnore]
    public virtual ICollection<Coupon> Coupons { get; set; }
    [XmlIgnore]
    public virtual ICollection<UserClaim> UserClaims { get; set; }
    [XmlIgnore]
    public virtual ICollection<UserLogin> UserLogins { get; set; }
    [XmlIgnore]
    public virtual ICollection<UserRole> UserRoles { get; set; }
}
当seriazlie在运行时出错

System.InvalidOperationException:生成XML文档时出错。-->System.InvalidOperationException:类型System.Data.Entity.DynamicProxies.User_43DB1D255133CC8990A023D6D7354697C201107F7F44D46786E12943B0163999不是预期的。使用XmlInclude或SoapInclude属性指定静态未知的类型

它是关于具有[XmlIgnore]的相关对象的

我试图消除关系,结果成功了


在序列化时忽略这些属性的解决方案是什么

正确的方法是使用DTO对象而不是实际模型的对象。是的,它看起来是正确的方法。非常感谢。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Avatar>()
            .HasMany(e => e.Users)
            .WithOptional(e => e.Avatar)
            .WillCascadeOnDelete();

        modelBuilder.Entity<User>()
            .HasMany(e => e.CouponApplicationUsers)
            .WithRequired(e => e.User)
            .HasForeignKey(e => e.ApplicationUser_Id);

        modelBuilder.Entity<User>()
            .HasMany(e => e.UserClaims)
            .WithOptional(e => e.User)
            .HasForeignKey(e => e.IdentityUser_Id);

        modelBuilder.Entity<User>()
            .HasMany(e => e.UserLogins)
            .WithOptional(e => e.User)
            .HasForeignKey(e => e.IdentityUser_Id);

        modelBuilder.Entity<User>()
            .HasMany(e => e.UserRoles)
            .WithOptional(e => e.User)
            .HasForeignKey(e => e.IdentityUser_Id);
    }
[WebMethod]        
public User GetUser(string userId)
{
    using (var context = new MemberContext())
    {
        var user = context.Users.FirstOrDefault(u => u.Id == userId);
        return user;
    }   
}