[SOLVED] SqlException: Invalid column name ‘CountryMasterCountryId’

Issue

This Content is from Stack Overflow. Question asked by siddhesh

I am doing CRUD operation using asp .net core 6 MVC (visual studio 2022). For this i complete County Create,Update,Delete,Read operation. but now i am trying cascading dropdown in state with relation county db. when i write “public virtual CountryMaster CountryMaster” and “public virtual ICollection StateMasters” in the models on state and country it give me error of sqlException:Invalid column name ‘CountryMasterCountryId’. here is Country model
”’

{
    [Key]
    public int CountryId { get; set; }

    [Display(Name = "Country Name")]
    [Required(ErrorMessage ="Please Enter Country")]
    public string CountryName { get; set; } = null!;

    [Display(Name ="When Entered")]
    public DateTime WhenEntered { get; set; } = DateTime.UtcNow;

    [Display(Name ="When Modified")]
    public DateTime WhenModified { get; set; }= DateTime.UtcNow;

    public virtual ICollection<StateMaster> StateMasters { get; set; }
} '''

here is State model
”’

{
    [Key]
    public int StateId { get; set; }

    [Required(ErrorMessage ="Please Enter State Name")]
    public string StateName { get; set; } = null!;

    
    [Display(Name = "Country Name")]
    public int CountryId { get; set; }

    [Display(Name = "When Entered")]
    public DateTime WhenEntered { get; set; } = DateTime.UtcNow;

    [Display(Name = "When Modified")]
    public DateTime WhenModified { get; set; } = DateTime.UtcNow;

    public virtual CountryMaster CountryMaster { get; set; } 
}

”’
What i do? Is Icollection different in .net core 6 MVC?



Solution

The properties you added to the Model does not exist in the database.
Try to update your database:

From the Tools menu, select NuGet Package Manager > Package Manager Console .

In the Package Manager Console (PMC), enter the following commands:

1: Change the the migration name

Add-Migration xxxx

2:Updates the database to the latest migration, which the previous command created

Update-Database


This Question was asked in StackOverflow by siddhesh and Answered by Qing Guo It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?