Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 484
johnny 5 Avatar asked Aug 15 '17 01:08

johnny 5


1 Answers

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");
like image 104
Ivan Stoev Avatar answered Oct 14 '22 00:10

Ivan Stoev