Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有自我关系的基本实体_C#_Asp.net Mvc_Entity Framework_Data Annotations - Fatal编程技术网

C# 具有自我关系的基本实体

C# 具有自我关系的基本实体,c#,asp.net-mvc,entity-framework,data-annotations,C#,Asp.net Mvc,Entity Framework,Data Annotations,我有一个基本实体类,如下所示: public abstract class BaseEntity { [System.ComponentModel.DataAnnotations.Key] [System.ComponentModel.DataAnnotations.Required] [Column("ID")] [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGe

我有一个基本实体类,如下所示:

 public abstract class BaseEntity
{
    [System.ComponentModel.DataAnnotations.Key]
    [System.ComponentModel.DataAnnotations.Required]
    [Column("ID")]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
    [System.ComponentModel.DataAnnotations.DataType("bigint")]
    public long id { get; set; }

    public virtual User createdBy { get; set; }

    public virtual User updatedBy { get; set; }
    public DateTime createdDate { get; set; }
    public DateTime updatedDate { get; set; }
}
还有一个用户类,例如:

public class User:BaseEntity
{
    public string userName { get; set; }


}
但是当我想将记录添加到用户表时,EF给了我一个错误。 错误是:

无法确定服务器之间关联的主体端 键入“mvcpracce.Models.User”和“mvcpracce.Models.User”。这个 必须使用显式配置此关联的主体端 关系可以是fluent API或数据批注


您是否缺少一个我必须像这样在BaseEntity类中添加外键的命令?[ForiegnKey]公共虚拟用户createdBy{get;set;}添加自引用我现在就知道了:无法确定依赖操作的有效顺序。依赖关系可能由于外键约束、模型要求或存储生成的值而存在。不确定,请发布新问题,可能适合于