Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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# 带有外键约束的EF迁移错误_C#_Entity Framework_Entity Framework Migrations - Fatal编程技术网

C# 带有外键约束的EF迁移错误

C# 带有外键约束的EF迁移错误,c#,entity-framework,entity-framework-migrations,C#,Entity Framework,Entity Framework Migrations,我得到了一个关于两个实体的约束的错误,这让我抓狂。我已经在谷歌上搜索、浏览并尝试了这些建议,但仍然没有成功。我得到的错误如下:- 引入外键约束 表上的“FK_dbo.UserTrackableItems_dbo.Users_ModifiedById” “UserTrackableItems”可能导致循环或多个级联路径。 指定在删除时不执行操作或在更新时不执行操作,或修改其他 外键约束。无法创建约束或索引。看见 以前的错误 我理解这个错误,但不知道在哪里修复它。我已经删除了[Required],我

我得到了一个关于两个实体的约束的错误,这让我抓狂。我已经在谷歌上搜索、浏览并尝试了这些建议,但仍然没有成功。我得到的错误如下:-

引入外键约束 表上的“FK_dbo.UserTrackableItems_dbo.Users_ModifiedById” “UserTrackableItems”可能导致循环或多个级联路径。 指定在删除时不执行操作或在更新时不执行操作,或修改其他 外键约束。无法创建约束或索引。看见 以前的错误

我理解这个错误,但不知道在哪里修复它。我已经删除了[Required],我已经尝试使用fluent API,我一定是做错了。这些是它提到的实体(其中一个是基类)

[表(“UserTrackableItems”)]
公共类UserTrackableItem:BaseEntity
{
[关键]
公共长UserTrackableItemId{get;set;}
[必需]
公共长用户标识{get;set;}
[外键(“用户ID”)]
公共虚拟用户用户{get;set;}
[必需]
public int TrackableItemId{get;set;}
[ForeignKey(“TrackableItemId”)]
公共虚拟TrackableItem TrackableItem{get;set;}
[必需]
公共int OrderBy{get;set;}
}
[表(“用户”)]
公共类用户:BaseEntity
{
[关键]
公共长用户标识{get;set;}
公共字符串名{get;set;}
公共字符串姓氏{get;set;}
公共字符串电子邮件{get;set;}
公共字符串密码{get;set;}
公共字符串哈希{get;set;}
公共字符串AvatarImage{get;set;}
公共日期时间?出生日期{get;set;}
public int?CountryId{get;set;}
自{get;set;}起的公共日期时间成员
公共bool IsAdmin{get;set;}
公共bool IsWriter{get;set;}
公共布尔IsStaff{get;set;}
公共bool被禁止{get;set;}
公共字符串OpenId{get;set;}
公共字符串OpenIdClaim{get;set;}
public ExternalAuthOrigin ExternalAuthOrigin{get;set;}
公共虚拟ICollection DailyTargets{get;set;}
公共虚拟ICollection度量目标{get;set;}
公共虚拟ICollection{get;set;}
公共虚拟ICollection可跟踪项{get;set;}
}
公共类基实体
{
公共日期时间添加数据{get;set;}
公共长加dbyid{get;set;}
[外键(“AddedById”)]
通过{get;set;}添加的公共虚拟用户
公共日期时间?删除日期{get;set;}
public long?DeletedById{get;set;}
[ForeignKey(“DeletedById”)]
公共虚拟用户DeletedBy{get;set;}
公共日期时间修改日期{get;set;}
公共长ModifiedById{get;set;}
[ForeignKey(“ModifiedById”)]
公共虚拟用户通过{get;set;}修改
}
[表(“可跟踪项”)]
公共类可跟踪项
{
[关键]
public int TrackableItemId{get;set;}
[必需]
公共字符串名称{get;set;}
公共字符串MeasurementName{get;set;}
公共虚拟ICollection用户{get;set;}
}

您可以添加类TrackableItem吗?刚刚更新以包含该类。我在下面发布了我的答案。这对你有用吗?对不起,回答你的回答。不幸的是没有。
[Table("UserTrackableItems")]
public class UserTrackableItem : BaseEntity
{
    [Key]
    public long UserTrackableItemId { get; set; }

    [Required]
    public long UserId { get; set; }
    [ForeignKey("UserId")]
    public virtual User User { get; set; }

    [Required]
    public int TrackableItemId { get; set; }
    [ForeignKey("TrackableItemId")]
    public virtual TrackableItem TrackableItem { get; set; }

    [Required]
    public int OrderBy { get; set; }
}

[Table("Users")]
public class User : BaseEntity
{
    [Key]
    public long UserId { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string Hash { get; set; }
    public string AvatarImage { get; set; }
    public DateTime? DateOfBirth { get; set; }
    public int? CountryId { get; set; }
    public DateTime MemberSince { get; set; }
    public bool IsAdmin { get; set; }
    public bool IsWriter { get; set; }
    public bool IsStaff { get; set; }
    public bool IsBanned { get; set; }

    public string OpenId { get; set; }
    public string OpenIdClaim { get; set; }
    public ExternalAuthOrigin ExternalAuthOrigin { get; set; }

    public virtual ICollection<DailyTarget> DailyTargets { get; set; }
    public virtual ICollection<SectionTarget> MealTargets { get; set; }
    public virtual ICollection<Section> Meals { get; set; }
    public virtual ICollection<UserTrackableItem> TrackableItems { get; set; }
}

    public class BaseEntity
{
    public DateTime AddedDate { get; set; }

    public long AddedById { get; set; }

    [ForeignKey("AddedById")]
    public virtual User AddedBy { get; set; }

    public DateTime? DeletedDate { get; set; }

    public long? DeletedById { get; set; }

    [ForeignKey("DeletedById")]
    public virtual User DeletedBy { get; set; }

    public DateTime ModifiedDate { get; set; }

    public long ModifiedById { get; set; }

    [ForeignKey("ModifiedById")]
    public virtual User ModifiedBy { get; set; }
}

[Table("TrackableItems")]
public class TrackableItem
{
    [Key]
    public int TrackableItemId { get; set; }

    [Required]
    public string Name { get; set; }

    public string MeasurementName { get; set; }

    public virtual ICollection<UserTrackableItem> Users { get; set; }

}