Would the Entity Framework's navigational properties work if I drop foreign key constraints from the database?

c# entity-framework entity-framework-6 sql

Question

As you know that developers mostly mock the relationship between tables instead of using physical relationships between table (yeah, the line drawn from one table to another if you put a foreign key constraint on the column).

But I believe that Entity Framework doesn't work properly if physical relationships aren't there for navigational properties.

So, is there any way around?

My classes:

public class Phones
{
        public int ID { get; set; }
        public string Model { get; set; }
        public string Manufacturer { get; set; }
        public List<Users> Users { get; set; }
}

public class Sims
{
        public int ID { get; set; }
        public string Code { get; set; }
}

This creates a 1-M relationship from User -> Sims.

But what if I drop the foreign key constraint and leave it as it is, how will the navigational properties work then?

1
0
1/15/2019 8:25:57 AM

Accepted Answer

At this case better to remove references from both classes and handle relations manually outside of these classes:

public class Sims
{
    public int ID { get; set; }
    public string Code { get; set; }

    //public User User { get; set; }
    public int UserID { get; set; }
}
1
1/15/2019 9:25:52 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