有下面的类,并尝试在数据库中查找记录返回错误.
使用C#,MVC 4,Entity Framework 4和SQL Server 2012数据库.
使用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的提示
然后代码看起来像这样.这个属性ForeignKey它是挂载链接和属性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; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。