简体   繁体   English

如果我从数据库中删除外键约束,那么实体框架的导航属性会起作用吗?

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

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. 但是我相信,如果导航属性不存在物理关系,则Entity Framework将无法正常工作。

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. 这将从用户->模拟用户创建1-M关系。

But what if I drop the foreign key constraint and leave it as it is, how will the navigational properties work then? 但是,如果我删除外键约束并保持原样,导航属性将如何工作呢?

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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM