简体   繁体   English

EfCore 3.1 尝试添加已存在的列

[英]EfCore 3.1 tries to add a column that already exists

I have a working .NET Core 2.2 project, which I just upgraded to .NET Core 3.1我有一个可用的 .NET Core 2.2 项目,我刚刚升级到了 .NET Core 3.1

There is something weird with EntityFrameworkCore going on and I don't know where to start EntityFrameworkCore 发生了一些奇怪的事情,我不知道从哪里开始

I have the following (simplified) model:我有以下(简化)模型:

public class Asset()
{
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   [Key]
   public Guid Id { get; set; }
   public Guid MainImageId{ get; set; }
   public Image MainImage { get; set; }
   public ICollection<Image> Images { get; set; }
}

public class Image()
{
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   [Key]
   public Guid Id { get; set; }
   public Guid AssetId { get; set; }
   public Asset Asset { get; set; }
}

So an Asset has 1 MainImage.所以一个 Asset 有 1 个 MainImage。

This is defined in my model like this:这在我的模型中定义如下:

modelBuilder.Entity<Image>().HasOne(x => x.Asset).WithMany(x => x.Images).OnDelete(DeleteBehavior.Restrict);

This definitely worked, until I upgraded.这绝对有效,直到我升级。 Now every time a SELECT happens on the Assets table, it is trying to select the column MainImageId1 , which doesn't exist.现在,每次在Assets表上发生 SELECT 时,它都会尝试选择不存在的列MainImageId1 If I try to AddMigration I see that EF Core will add a column called MainImageId1 and put a foreign key in place to the Images table.如果我尝试AddMigration我会看到 EF Core 将添加一个名为MainImageId1的列,并将外键放置到Images表中。

I have tried adding the right column name to this property, but this also did not work:我曾尝试向此属性添加正确的列名,但这也不起作用:

modelBuilder.Entity<Asset>().Property(x => x.MainImageId).HasColumnName("MainImageId");

Does anyone have any idea how I can debug this further?有谁知道我如何进一步调试它? Is something wrong perhaps in my DbContext class?我的 DbContext 类可能有问题吗?

I eventually found the answer.我终于找到了答案。 I was missing a property, which seems to be required to make sure Entity Framework understands the relationship.我缺少一个属性,这似乎是确保实体框架理解这种关系所必需的。

public class Asset()
{
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   [Key]
   public Guid Id { get; set; }
   public Guid MainImageId{ get; set; }
   public Image MainImage { get; set; }
   public ICollection<Image> Images { get; set; }
}

public class Image()
{
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   [Key]
   public Guid Id { get; set; }
   public Guid AssetId { get; set; }
   public Asset Asset { get; set; }

   //THIS WAS ADDED TO SOLVE THE PROBLEM:
   public ICollection<Asset> MainAssets {get;set;}
}

I added the collection MainAssets to the Images class and then updated my model to include the following two mappings:我将MainAssets集合MainAssets到 Images 类,然后更新我的模型以包含以下两个映射:

modelBuilder.Entity<Image>()
    .HasOne(x => x.Asset)
    .WithMany(x => x.Images)
    .HasForeignKey(x => x.AssetId);

modelBuilder.Entity<Asset>()
    .HasOne(x => x.MainImage)
    .WithMany(x => x.MainAssets)
    .HasForeignKey(x => x.MainImageId);

And now everything works fine again.现在一切正常。 It seems that by adding the collection of MainAssets was key to having Entity framework understand the relationship better.似乎通过添加MainAssets的集合是让 Entity 框架更好地理解关系的关键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM