Tie*_*ech 6 .net c# entity-framework asp.net-mvc-4
有下面的类,并尝试在数据库中查找记录返回错误.使用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.
Run Code Online (Sandbox Code Playgroud)
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; }
}
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
经过大量搜索后,我发现了一个关于使用的提示,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; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2855 次 |
| 最近记录: |