我在我的webservice项目中使用EF codefirst和现有数据库。我有User对象。
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; }
}
这是我的对象关系:
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);
}
Member.asmx:
[WebMethod]
public User GetUser(string userId)
{
using (var context = new MemberContext())
{
var user = context.Users.FirstOrDefault(u => u.Id == userId);
return user;
}
}
我在运行时的seriazlie上遇到错误。
“System.InvalidOperationException:生成XML文档时出错.---&gt; System.InvalidOperationException:不期望类型为System.Data.Entity.DynamicProxies.User_43DB1D255133CC8990A023D6D7354697C201107F7F44D46786E12943B0163999。使用XmlInclude或SoapInclude属性指定类型这是静态不知道的。“
关于 [XmlIgnore]
的相关对象我试图删除关系,但它有效。
在序列化中忽略这些属性的解决方案是什么?