首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

EF中,关于外键挑动循环报错的疑惑

2012-09-10 
EF中,关于外键引发循环报错的疑惑C# codepublic class User{[Key][Display(Name 工号)][Required(Erro

EF中,关于外键引发循环报错的疑惑

C# code
public class User    {        [Key]        [Display(Name = "工号")]        [Required(ErrorMessage = "*")]        [StringLength(15, ErrorMessage = "不能超过15个字符")]        public string UserNo { get; set; }        [Display(Name = "密码")]        [Required(ErrorMessage = "*")]        [StringLength(20, ErrorMessage = "不能超过20个字符")]        [DataType(DataType.Password)]        public string Password { get; set; }        [Display(Name = "姓名")]        [Required(ErrorMessage = "*")]        [StringLength(10, ErrorMessage = "不能超过10个字符")]        public string RealName { get; set; }        [Display(Name = "性别")]        [Required(ErrorMessage = "*")]        public string Sex { get; set; }        [Display(Name = "出生年月")]        [Required(ErrorMessage = "*")]        public DateTime? Birthday { get; set; }        [Display(Name = "身份证号码")]        public string IDCardNumber { get; set; }        [Display(Name = "身高")]        public int? Height { get; set; }        [Display(Name = "照片")]        public string PhotoUrl { get; set; }        /// <summary>        /// 是否残疾         /// </summary>        [Display(Name = "残疾人")]        public bool IsDisabled { get; set; }        [Display(Name = "国家")]        [Required(ErrorMessage = "*")]        public int Country { get; set; }        [Display(Name = "户口所在地")]        public string DomicilePlace { get; set; }        /// <summary>        /// 婚姻状态         /// Y已婚        /// N未婚        /// </summary>        [Display(Name = "婚姻状态")]        public string IsMarried { get; set; }        [Display(Name = "政治面貌")]        [Required(ErrorMessage = "*")]        public int PoliticsStatus { get; set; }                [Display(Name = "学历")]        public int? Education { get; set; }        [Display(Name = "毕业学校")]        public string GraduateSchool { get; set; }        [Display(Name = "最高学位")]        public string Degree { get; set; }        [Display(Name = "获得学校")]        public string DegreeBySchool { get; set; }        [Display(Name = "居住地址")]        public string ResidentialAddress { get; set; }        [Display(Name = "邮编")]        public string Post { get; set; }        [Display(Name = "手机号码")]        public string TelPhoneNumber { get; set; }        [Display(Name = "电子邮箱")]        public string Email { get; set; }        [Display(Name = "QQ号码")]        public string QQ { get; set; }        [Display(Name = "MSN")]        public string MSN { get; set; }        [Display(Name = "入职日期")]        [Required(ErrorMessage = "*")]        public DateTime? JoinDate { get; set; }        [Display(Name = "职位")]        [Required(ErrorMessage = "*")]        public int PositionID { get; set; }        [ForeignKey("PositionID")]        public virtual Position Position { get; set; }        /// <summary>        /// 在职/兼职/离退/实习        /// </summary>        [Display(Name = "员工状态")]        public string State { get; set; }        [Display(Name = "工资类型")]        [Required(ErrorMessage = "*")]        public int PayType { get; set; }        [Display(Name = "考核类型")]        [Required(ErrorMessage = "*")]        public int CheckType { get; set; }        /// <summary>        /// 是否 超级用户         /// 1是        /// 0否        /// </summary>        [Display(Name = "用户类型")]        public string IsSuper { get; set; }        /// <summary>        /// 是否启用        /// 0启用        /// 1禁用        /// </summary>        [Display(Name = "状态")]        public string IsEnable { get; set; }                [Display(Name = "备注")]        [StringLength(200, ErrorMessage = "不能超过200个字符")]        public string Remark { get; set; }    } 




C# code
public class Interview    {        [Key]        public int InterviewID { get; set; }        [Display(Name = "员工")]        [Required(ErrorMessage = "*")]        public string UserNo { get; set; }        [ForeignKey("UserNo")]        public virtual User User { get; set; }        [Display(Name = "访谈时间")]        [Required(ErrorMessage = "*")]        public DateTime InterviewDate { get; set; }        [Display(Name = "访谈人")]        [Required(ErrorMessage = "*")]        public string InterviewUserNo { get; set; }        /*        [ForeignKey("InterviewUserNo")]        public virtual User InterviewUser { get; set; }        */        [Display(Name = "访谈事由")]        public string Reason { get; set; }        [Display(Name = "访谈内容")]        public string Content { get; set; }        [Display(Name = "访谈发现")]        public string Result { get; set; }        [Display(Name = "附件")]        public string Attachment { get; set; }        [Display(Name = "备注")]        [StringLength(200, ErrorMessage = "不能超过200个字符")]        public string Remark { get; set; }    }


第一个是User实体,第二个是访谈实体,该实体中有 被访谈人 和 访谈人。

请问,访谈实体中为什么不能设置2个对UserID的外键呢? (UserNo和InterviewUserNo)
目前注释了InterviewUserNo的外键关联,否则报循环引用的错误。



[解决办法]
昨晚我发了类似的帖子。
没直接解决的办法。

我绕过去了,我的问题是序列化为json的时候报告循环引用的错误。
从表包含主表的属性,去掉可序列化的标记。

这样处理的json 字符串,主表包含从表,从表不包含主表。

我纳闷,你的实体类怎么跟我的不一样?你怎么生成的?
[解决办法]
你试试把 public virtual User 
改为
private virtual User
或者private User

热点排行