Hi @Cenk ,
How with EF Core if models have no relations?
If you don't configure the relationship via EF core, you can use LINQ left outer joins to join the table and get the related entities information.
For example, I created the following model:
public class Order
{
public int OrderId { get; set; }
public int OrderNumber { get; set; }
public string ClientName { get; set; }
public string SupplierName { get; set; }
public string ShipmentNumber { get; set; }
public string EmployeeName { get; set; }
public string OrderStatus { get; set; }
public DateTime Date { get; set; }
public string Description { get; set; }
}
public class Client
{
public int ClientId { get; set; }
public string ClientName { get; set; }
public string TaxAdministration { get; set; }
public string Address { get; set; }
public string Telephone { get; set; }
public string email { get; set; }
public string ResponsibleName { get; set; }
public string Responsibleemail { get; set; }
public string Description { get; set; }
}
public class Supplier
{
public int SupplierId { get; set; }
public string SupplierName { get; set; }
public string Address { get; set; }
public string Telephone { get; set; }
public string email { get; set; }
public string ResponsibleName { get; set; }
public string Responsibleemail { get; set; }
public string Description { get; set; }
}
Then, add the ApplicationDbContext as below:
public class ApplicationDbContext : IdentityDbContext
{
public DbSet<Order> Orders { get; set; }
public DbSet<Client> Clients { get; set; }
public DbSet<Supplier> Suppliers { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
Then, enable migration to generate the relate table in the database and add the test data:
After that, in the controller, perform left outer joins to get the orders and its related clients and supplier information, if the clients or supplier does not exist, it will show null.
View the code from here: 213811-linq.txt
The result is as below:
The output model is like this, you can add other information.
public class Output
{
public int OrderNumber { get; set; }
public string OrderDate { get; set; }
public string ClientName { get; set; }
public string Client_Address { get; set; }
public string SupplierName { get; set; }
public string Suoplier_Email { get; set; }
}
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.
Best regards,
Dillion