Get related data without relationship between tables

entity-framework-core entity-framework-core-2.1

Question

When I have two models with one to many relationship, they look like the models below:

public class Student
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int StudentID { get; set; }

    public string Name { get; set; }

    public int ClassID { get; set; }
    [ForeignKey("ClassID")]
    public virtual Class Class { get; set; }
}

public class Class
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ClassID { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

This means that I can use the include method to get the related data. Now I work on a project with more than 600 tables. For these tables there are no relationships and I can not create relationships between the tables. Is there a way to link the models somehow so that I will be able to get the related data?

1
0
7/6/2018 5:53:00 AM

Popular Answer

Yes, you can join tables without Relationships in Model Classes.

For example assume you have TeacherId in both of your tables Student and Class

You can join two tables based on TeacherId as follows

DbContext.Students
.Join(DbContext.Classes,student=>student.TeacherId,class=>class.TeacherId, (std,tchr)=> new {std,tchr});
1
5/9/2019 7:31:00 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow