实体框架核心:无效的列名

时间:2018-07-09 16:34:21

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

我有这个数据库模型:

public partial class Image
{
    public Image()
    {
        EntityImages = new List<EntityImage>();
    }

    public int Id { get; set; }
    public string Content { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public string ModifiedBy { get; set; }
    public DateTime? ModifiedDate { get; set; }
    public bool IsDeleted { get; set; }

    public ICollection<EntityImage> EntityImages { get; set; }
}

public partial class EntityImage
{
    public int Id { get; set; }
    public int ImageId { get; set; }
    public int EntityId { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public string ModifiedBy { get; set; }
    public DateTime? ModifiedDate { get; set; }
    public bool IsDeleted { get; set; }

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

    [ForeignKey("ImageId")]
    public Image Image { get; set; }
}

我首先使用了数据库,当我尝试在Images表中进行插入时,出现“ Invalid column name:EntityId”错误。 这是上下文代码:

modelBuilder.Entity<EntityImage>(entity =>
        {
            entity.Property(e => e.CreatedBy).HasMaxLength(50);

            entity.Property(e => e.CreatedDate).HasColumnType("datetime");

            entity.Property(e => e.ModifiedBy).HasMaxLength(50);

            entity.Property(e => e.ModifiedDate).HasColumnType("datetime");

            entity.HasOne(d => d.Entity)
                .WithMany(p => p.EntityImages)
                .HasForeignKey(d => d.EntityId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_CategorySubcategory_Entities");

            entity.HasOne(d => d.Image)
                .WithMany(p => p.EntityImages)
                .HasForeignKey(d => d.ImageId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_CategorySubcategory_Images");
        });

modelBuilder.Entity<Image>(entity =>
        {
            entity.Property(e => e.Content)
                .IsRequired()
                .HasMaxLength(50);

            entity.Property(e => e.CreatedBy).HasMaxLength(50);

            entity.Property(e => e.CreatedDate).HasColumnType("datetime");

            entity.Property(e => e.ModifiedBy).HasMaxLength(50);

            entity.Property(e => e.ModifiedDate).HasColumnType("datetime");
        });

我不知道错误在哪里。 我不知道为什么Entity Framework尝试插入带有EntityId列的图像记录,因为该列在该表中不存在。

对于类别,子类别和CategoriesSubcategories表,我具有相同的模型,并且代码可以正常工作。

0 个答案:

没有答案