Entity Framework Core仍然使用旧列 [英] Entity Framework Core still picks up old column

查看:78
本文介绍了Entity Framework Core仍然使用旧列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从表中删除了列ConversationId.当我开始调试服务并尝试保存时,出现错误:

I recently delete a column ConversationId from my tables. When I start to debug my service and try to save I am getting an error:

无效的列名"ConversationId".

Invalid column name 'ConversationId'.

代码:

public class AstootContext : DbContext
{
    public AstootContext(DbContextOptions<AstootContext> options)
        : base(options)
    { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }

    public DbSet<ServiceRequest> ServiceRequests { get; set; }
}

我的实体看起来像这样:

And my entity looks like this:

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的引用都已从代码中删除,我已经进行了重建,但是仍然出现此错误,我也不明白为什么.

All references to ConversationId were removed from the code, I've rebuilt, yet I'm still getting this error and I don't understand why.

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

This is my SQL Server table as you can see there is no ConversationId:

是否有需要删除的秘密缓存,或者必须运行某些东西来更新此秘密?

Is there a secret cache that I need to delete or something I have to run to update this?

推荐答案

EF Core是基于代码的ORM,其中最重要的是M-Mapper.实际的数据库结构是什么都没有关系,重要的是EF *会基于您的代码模型(实体类及其属性,以及数据注释,流利的配置和约定集)来考虑EF.

EF Core is code based ORM, with the most important here being the M - Mapper. It doesn't matter what the actual database structure is, the important is what EF *thinks** it is based on your code model (entity classes and their properties, combined with data annotations, fluent configuration and set of conventions).

因此,问题应该源于代码.由于您删除了 explicit 属性,因此应由阴影属性.而且,如文档链接中所述, shadow 属性通常是通过约定从关系中引入的:

So the problem should originate from code. Since you've removed the explicit property, it should be caused by shadow property. And as explained in the documentation link, shadow properties are usually introduced by convention from relationships:

可以通过约定在发现关系但在从属实体类中未找到任何外键属性的情况下创建阴影属性.在这种情况下,将引入影子外键属性.

Shadow properties can be created by convention when a relationship is discovered but no foreign key property is found in the dependent entity class. In this case, a shadow foreign key property will be introduced.

文档还解释了在不同情况下应用的命名规则.

The documentation also explains the naming rules applied in different scenarios.

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

A shadow property called ConversationId can be introduced in a several ways, but according to the provided information, the most likely cause is to have an entity class called Conversation defining one-to-many relationship with ServiceRequest by having a collection type navigation property:

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

根据您的评论确实如此.

Which according to your comment was indeed the case.

出于完整性考虑,以下是生成此类属性的其他一些可能的方案:

For completeness, here are some other possible scenarios generating such property:

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

(1) No collection navigation property in Conversation, reference navigation property in ServiceRequest:

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

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

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

(2) No navigation properties in Conversation and ServiceRequest, fluent configuration:

modelBuilder.Entity<Conversation>()
    .HasMany<ServiceRequest>();

modelBuilder.Entity<ServiceRequest>()
    .HasOne<Conversation>();

或以上内容的变体.

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

(3) No relationship involved, shadow property created through fluent configuration:

modelBuilder.Entity<ServiceRequest>()
    .Property<int>("ConversationId");

这篇关于Entity Framework Core仍然使用旧列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆