entity framework核心仍然采用旧列

我最近从表中删除了一个ConversationId列。 当我开始调试我的服务并尝试保存时,我收到一个错误:

列名称’ConversationId’无效。

码:

 public class AstootContext : DbContext { public AstootContext(DbContextOptions options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { } public DbSet ServiceRequests { get; set; } } 

我的实体看起来像这样:

 public class ServiceRequest { public int Id { get; set; } public int SenderUserId { get; set; } public int PriceTypeId { get; set; } public decimal Price { get; set; } public bool IsAccepted { get; set; } public DateTime Created { get; set; } public int MessageId { get; set; } } 

所有对ConversationId引用都从代码中删除了,我已经重建了,但我仍然遇到这个错误,我不明白为什么。

这是我的SQL Server表,因为您可以看到没有ConversationId

在此处输入图像描述

是否有需要删除的秘密缓存或我必须运行以更新此缓存?

EF Core是基于代码的ORM,其中最重要的是M-Mapper。 实际数据库结构是什么并不重要,重要的是EF *认为**它基于您的代码模型(实体类及其属性,结合数据注释,流畅配置和约定集)。

所以问题应该来自代码。 由于您已删除显式属性,因此应该由shadow属性引起。 正如文档链接中所解释的, 影子属性通常通过约束来引入关系:

当发现关系但在依赖实体类中找不到外键属性时,可以按约定创建阴影属性。 在这种情况下,将引入影子外键属性。

该文档还解释了在不同场景中应用的命名规则。

可以通过多种方式引入名为ConversationId的shadow属性,但根据提供的信息,最可能的原因是有一个名为Conversation的实体类通过具有集合类型导航属性来定义与ServiceRequest的一对多关系:

 public class Conversation { public int Id { get; set; } // ... public ICollection ServiceRequests { get; set; } } 

根据你的评论确实如此。

为了完整起见,以下是生成此类属性的一些其他可能方案:

(1) Conversation没有集合导航属性, ServiceRequest引用导航属性:

 public class Conversation { public int Id { get; set; } // ... } public class ServiceRequest { // ... public Conversation Conversation { get; set; } } 

(2) ConversationServiceRequest没有导航属性,流畅的配置:

 modelBuilder.Entity() .HasMany(); 

要么

 modelBuilder.Entity() .HasOne(); 

或以上的变化。

(3)不涉及任何关系,通过流畅的配置创建阴影属性:

 modelBuilder.Entity() .Property("ConversationId");