【发布时间】:2016-02-28 21:17:04
【问题描述】:
拥有下面的类,并尝试在数据库中查找记录返回错误。 使用 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; }
}
数据库外键
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);
【问题讨论】:
-
外键是否存在?
-
数据库@Papa中存在外键
-
有代码优先和数据库优先的方法。不过我最喜欢的是Read first 方法
-
@marlon.tiedt 您期望的数据库结构是什么?你能说一下你想要达到什么类型的关系吗?一对多/一/零?
-
关系是一对一的@Shyju
标签: c# .net entity-framework asp.net-mvc-4