c# – 无法确定类型之间关联的主要结尾 – 实体框架错误,同一类
发布时间:2020-12-15 08:47:58 所属栏目:百科 来源:网络整理
导读:有下面的类,并尝试在数据库中查找记录返回错误. 使用C#,MVC 4,Entity Framework 4和SQL Server 2012数据库. 错误 Unable to determine the principal end of an association between the types 'FlexApp.Models.Model.Usuario' and 'FlexApp.Models.Model.Us
有下面的类,并尝试在数据库中查找记录返回错误.
使用C#,MVC 4,Entity Framework 4和SQL Server 2012数据库. 错误 Unable to determine the principal end of an association between the types 'FlexApp.Models.Model.Usuario' and 'FlexApp.Models.Model.Usuario'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. 类 public class Usuario { [Key] public int UsuarioID { get; set; } public string Nome { get; set; } public int UsuCad { get; set; } public int UsuAlt { get; set; } [ForeignKey("UsuCad")] public virtual Usuario UsuarioCad { get; set; } [ForeignKey("UsuAlt")] public virtual Usuario UsuarioAlt { get; set; } } DataBase FK alter table USUARIO add constraint USUARIO_fk01 foreign KEY(UsuCad) REFERENCES USUARIO(UsuarioID); alter table USUARIO add constraint USUARIO_fk02 foreign KEY(UsuAlt) REFERENCES USUARIO(UsuarioID); 解决方法
答复
经过大量搜索,我找到了关于使用InverseProperty的提示 谢谢您的帮助 public class Usuario { [Key] public int UsuarioID { get; set; } public string Nome { get; set; } public int UsuCad { get; set; } public int UsuAlt { get; set; } [ForeignKey("UsuCad")] [InverseProperty("UsuarioID")] public virtual Usuario UsuarioCad { get; set; } [ForeignKey("UsuAlt")] [InverseProperty("UsuarioID")] public virtual Usuario UsuarioAlt { get; set; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |