简体   繁体   English

EF外键设置

[英]EF Foreign Key setup

I have a question regarding the setup of foreign keys in entity framework 6. Our project stores data from a few other services (to have faster access to the data) and provides the users with charts and statistics depending on the stored data. 我有一个关于在实体框架6中设置外键的问题。我们的项目存储了来自其他一些服务的数据(以便更快地访问数据),并根据存储的数据向用户提供图表和统计信息。 For the storage of the data we´ve setup a cronjob which runs daily at about 3 AM. 为了存储数据,我们设置了一个cronjob,它每天大约在凌晨3点运行。

Here are 2 example database models: 这是2个示例数据库模型:

public class Project {
    public string Id { get; set; }
    public string Title { get; set; }
}

public class Issue {
    public string Id { get; set; }
    public string Title { get; set; }
    [ForeignKey("Project")]
    public string ProjectId { get; set; }
    [ForeignKey("ProjectId")]
    public Project Project { get; set; }
}

The problem now is for some issues we don´t save the project it depends on but we have to save the ProjectId (because at a later point it might be possible that the project exists in our database). 现在的问题是,某些问题我们没有保存它依赖的项目,但是我们必须保存ProjectId(因为稍后可能该项目存在于我们的数据库中)。 So when I try to save this issues it tells me that I can´t save them because the project does not exist. 因此,当我尝试保存此问题时,它告诉我由于项目不存在,我无法保存它们。

Is there any way I can tell entity framework that it doesn´t matter if the project exists or not? 我有什么办法可以告诉实体框架项目是否存在无关紧要? Currently I´ve just removed the ForeignKeys but this makes it very slow when I try to get the full list of issues with their projects. 目前,我刚刚删除了ForeignKey,但是当我尝试获取与他们的项目有关的问题的完整列表时,这使它非常慢。

Or is there any other way to read out all issues with their projects if there are no foreign keys? 或者,如果没有外键,是否还有其他方法可以读出项目中的所有问题? Currently I´m using a foreach loop to go threw each issue and then I search for the project but with more than 10.000 issues this get´s very slow. 目前,我正在使用一个foreach循环来抛出每个问题,然后我搜索该项目,但问题超过10.000个,则进展非常缓慢。

The navigation property you've defined is requiring the data in the Project table in order to save an Issue . 您定义的导航属性需要Project表中的数据才能保存Issue This isn't Entity Framework, this is a SQL Server foreign key constraint issue. 这不是实体框架,这是SQL Server外键约束问题。 Entity Framework is doing preliminary validation to not waste a connection that will ultimately fail. 实体框架正在进行初步验证,以免浪费最终将失败的连接。 While you can turn off enforcing the foreign key constraint, there is not a good way to turn this validation off in Entity Framework 虽然您可以关闭强制外键约束,但是没有一种好的方法可以在Entity Framework中关闭此验证

Keep in mind, having a foreign key does not mean it will help with your query's performance. 请记住,拥有外键并不意味着它将有助于查询的性能。 It's simply a way to enforce referential integrity. 这只是强制执行参照完整性的一种方法。 I suspect that your real problem is the way you've written your query. 我怀疑您的真正问题是编写查询的方式。 Without seeing your query and metrics around "slow", it be hard to point you in the right direction. 如果看不到围绕“慢”的查询和指标,就很难为您指明正确的方向。

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

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