Entity Framework Core still picks up old column

前端 未结 1 1023
傲寒
傲寒 2020-12-16 02:39

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

相关标签:
1条回答
  • 2020-12-16 02:46

    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");
    
    0 讨论(0)
提交回复
热议问题