Question

Say we have 2 tables

public class Table1
{
    [Key]
    public int ID { get; set; }

    public int ID2 { get; set; }

    public int ID3 { get; set; }
}

public class Table2
{
    [Key]
    public int ID { get; set; }

    public int ID2 { get; set; }

    public int ID3 { get; set; }
}

Column ID is primary key in Table1 and in Table2, and that's it!

Now, I want to create a relationship ( not in the database!); I just want to lie to EntityFramework that there is a relationship between these 2 tables.

So, I want ID2 from Table2 to be foreign key and reference not column ID from Table1, but column ID2.

Is that possible?

EDIT2

I know is possible something like

public class Table2
{
    //...

    public int ID2 { get; set; }

    [ForeignKey("ID2")]
    public Table1 Table1 { get; set; }

    //...
}

and

public class Table1
{

    //...

    public virtual ICollection<Table2> Table2 { get; set; }

    //...
}

I successfully expanded all Table2 rows for a certain Table1 row.

The join of the sql will be like: Table1.ID = Table2.ID2; I need it to be like Table1.ID2 = Table2.ID2

Was it helpful?

Solution

I end up creating 2 views for the A table and mapping those views to EF Entities. First view has ID as primary key, second view has ID2 as primary key. Obviously, second view was the one used to expand all Table2 rows.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top