解决-EF关系孩子有很多父母,父母有很多孩子

时间:2020-03-22 03:25:54

标签: c# asp.net-core entity-framework-core

构建一个动态表单,用户可以在其中创建实体及其字段。 EntityField可能的dataTypeId可以是EntityId,以便查找可以是Entity。

   public class Entity : Base, IBase
   {
    public Entity()
    {
        EntityFields = new HashSet<EntityField>();
        PickFromEntity = new HashSet<EntityField>();
    }
    public Guid Id { get; set; }

    [StringLength(255)]
    [Required(AllowEmptyStrings = false)]
    [Remote("CheckDuplicate", "Entity", HttpMethod = "POST")]
    public string Name { get; set; }

    public virtual ICollection<EntityField> EntityFields { get; set; }
    public virtual ICollection<EntityField> PickFromEntity { get; set; }
}


 public class EntityField : Base, IBase
{
    public EntityField()
    {
        EntityChildren = new HashSet<Entity>();
    }

    public Guid Id { get; set; }
    public Guid EntityId { get; set; }      //Field for What Entity

    [StringLength(255)]
    [Required(AllowEmptyStrings = false)]
    public string Name { get; set; }

    [StringLength(1000)]
    public string HelpDescription { get; set; }
    public short Order { get; set; }
    public Guid DataTypeId { get; set; }
    public Nullable<Guid> PickListId { get; set; }      //if DAtatype is Picklist 
    public Nullable<Guid> PickFromEntityId { get; set; } //if DAtatype is Entity
    public Nullable<int> Minimum { get; set; }      //Will ast as Total rows for Data Type Table
    public Nullable<int> Maximum { get; set; }      //Will ast as Total Columns for Data Type Table
    public bool IsVisibleOnSelect { get; set; } = false;   //Only for Entity Data Type
    public bool IsRequired { get; set; } = false;

    public string Format { get; set; }
    public string DefaultValue { get; set; }

    [ForeignKey("PickFromEntityId")]
    public virtual Entity PickFromEntity { get; set; }

    [ForeignKey("PickListId")]
    public virtual PickList PickList { get; set; }

    [ForeignKey("EntityId")]
    public virtual Entity Entity { get; set; }

    [ForeignKey("DataTypeId")]
    public virtual ListOfValue DataType { get; set; }

    public ICollection<Entity> EntityChildren { get; set; }
}

迁移引发错误: 无法确定由类型为“ ICollection”的导航属性“ Entity.PickFromEntity”表示的关系。要么手动配置关系,要么使用“ [NotMapped]”属性或“ OnModelCreating”中的“ EntityTypeBuilder.Ignore”忽略此属性。

0 个答案:

没有答案