Search code examples
c#entity-frameworkentity-framework-coreef-code-firstef-core-3.0

mapping between two tables using EF Core Code first


I am looking to try to build the schema and relation between below two models using EF Core code first like below

public class Status
{
    public int Id { get; set; }
    public string Status { get; set; }
}

public class Order
{
    public Guid Id  { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public Status status { get; set; }
}

Could any one please suggest is there any changes need to be done to the above models (Status & Order) to get the query results like below

     OrderID    description    status
       1            test1      approved
       2            test2      rejected

Solution

  • Make your navigation property "Status" to virtual property like:

    public class Status
    {
        public int Id { get; set; }
        public string Status { get; set; }
    }
    
    public class Order
    {
        public Guid Id  { get; set; }
        public string Name { get; set; }
        public string Description { get; set; } 
        public int StatusId { get; set; }
        public virtual Status status { get; set; }
    } 
    

    Then you can do Order.Status.Status for each row while displaying