Moved to EF Core no longer able to find column

JSpencer 0 Reputation points
2023-09-13T19:52:45.7533333+00:00

I have recently migrated my project from .net framework 4.7.2 to .net core - net6.0 and it seems EF core can no longer see the auto generated FK columns that "old" EF created. "Old" EF created them with an underscore, e.g RetailerReview_Id but EF Core is expecting a column name to be RetailerReviewId. This is code first.

This is my parent model:

[Table("fooRetailerReview")]
public class RetailerReview
{
    [Key]
    public int Id { get; set; }

    public string RetailerId { get; set; }

    [Range(1, 5)]
    public int RetailerRating { get; set; }

    [Range(1, 5)]
    public int ProductRating { get; set; }

    public string Title { get; set; }

    [StringLength(500)]
    public string Content { get; set; }

    public string Response { get; set; }

    public ICollection<RetailerReviewImage> Images { get; set; }

    [StringLength(500)]
    public string LowRatingReason { get; set; }

    public virtual ICollection<RetailerReviewImage> ConnectRetailerReviewImages { get; set; } = new List<RetailerReviewImage>();
}

And this is the ICollection for RetailerReviewImage linked model:

[Table("fooRetailerReviewImage")]
public class RetailerReviewImage
{
    [Key]
    public int Id { get; set; }

    public int ContentLinkId { get; set; }

    public ApprovalState ApprovalState { get; set; }

    public string RejectionReason { get; set; }

    public int Review_Id { get; set; }

    public DateTime DateLastModified { get; set; }
}

The Db tables were automatically created as well as the FK Column which on fooRetailerReviewImage table is RetailerReview_Id:

User's image

Before moving to EF core this was all working, but now when running (and debugging) I get the following error:

Invalid column name 'RetailerReviewId1'.

Invalid column name 'RetailerReviewId'.

Invalid column name 'RetailerReviewId1'.

Like I said, nothing else has changed. Can anyone assist or do I need to raise a bug?

Developer technologies | .NET | Entity Framework Core
Developer technologies | .NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Wenbin Geng 741 Reputation points Microsoft External Staff
    2023-09-14T10:04:54.7666667+00:00

    Hi @JSpencer, Welcome to Microsoft Q&A,

    Update:

    Since you have a firm reason for wanting to preserve the original database as much as possible, recreating a project is no longer an option for you.

    I think one solution you can take at this time is the CodeFirst migration of EF Core, which will generate corresponding entity classes and relationships between entities based on your database.

    For details, please refer to: https://learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding/?tabs=dotnet-core-cli

    Best Regards,

    Wenbin


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.