Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

c# - Entity Framework Core still picks up old column

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:

Invalid column name 'ConversationId'.

Code:

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; }
}

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.

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

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

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.

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) 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) No navigation properties in Conversation and ServiceRequest, fluent configuration:

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

or

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

or variations of the above.

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...