- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已经花了几天时间,试图解决这个问题。在制作一个简单的项目来举例说明我的问题时,我偶然发现了一个可能的解决方案。所以,这是一个双重问题。
但首先,了解一些背景信息:
我刚刚开始使用 Entity Framework 4.1 (EF) 和 Code First 为我的 ASP.NET MVC 项目创建模型。我需要一些与此类似的模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestApp.Models
{
public class Family
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Father> Fathers { get; set; }
public virtual ICollection<Mother> Mothers { get; set; }
}
public class Mother
{
public int ID { get; set; }
public string Name { get; set; }
public int FamilyID { get; set; }
public virtual ICollection<Child> Children { get; set; }
public virtual Family Family { get; set; }
}
public class Father
{
public int ID { get; set; }
public string Name { get; set; }
public int FamilyID { get; set; }
public virtual ICollection<Child> Children { get; set; }
public virtual Family Family { get; set; }
}
public class Child
{
public int ID { get; set; }
public string Name { get; set; }
public int MotherID { get; set; }
public int FatherID { get; set; }
public virtual Mother Mother { get; set; }
public virtual Father Father { get; set; }
}
}
和 DbContext:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace TestApp.Models
{
public class TestContext : DbContext
{
public DbSet<Family> Families { get; set; }
public DbSet<Mother> Mothers { get; set; }
public DbSet<Father> Fathers { get; set; }
public DbSet<Child> Children { get; set; }
}
}
(请原谅这个蹩脚的例子,这就是我周五炸毁的大脑能够想出的。)
一个家庭可以有多个母亲和多个父亲。一个 child 有一个母亲和一个父亲。我咨询了我工作中的一位 .NET 专家,他同意这没有什么特别之处。至少就我们所见而言。
但是当我运行代码时,我得到了这个异常:
System.Data.SqlServerCe.SqlCeException: The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = Mother_Family ]
我确实看到了这个循环:家庭 - 母亲 - child - 父亲 - 家庭
。但是,如果我自己创建数据库表(我不喜欢这样做,这就是我喜欢 Code First 的原因),据我所知,它将是一个完全有效的数据结构。
那么,我的第一个问题是:为什么先使用代码会出现这个问题?有没有办法告诉 EF 如何正确处理循环?
然后,正如我最初写的那样,在创建一个简单的项目来举例说明我的问题时,我偶然发现了一个可能的解决方案。在定义我的模型时,我只是忘记了一些属性。为了在下面的示例中清楚起见,我没有删除它们,而是注释掉了我忘记的模型部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestApp.Models
{
public class Family
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Father> Fathers { get; set; }
public virtual ICollection<Mother> Mothers { get; set; }
}
public class Mother
{
public int ID { get; set; }
public string Name { get; set; }
// public int FamilyID { get; set; }
public virtual ICollection<Child> Children { get; set; }
public virtual Family Family { get; set; }
}
public class Father
{
public int ID { get; set; }
public string Name { get; set; }
// public int FamilyID { get; set; }
public virtual ICollection<Child> Children { get; set; }
public virtual Family Family { get; set; }
}
public class Child
{
public int ID { get; set; }
public string Name { get; set; }
// public int MotherID { get; set; }
// public int FatherID { get; set; }
public virtual Mother Mother { get; set; }
public virtual Father Father { get; set; }
}
}
因此,删除这些 SomethingID
引用属性似乎可以解决我的问题。正如您在本文末尾链接到的示例项目的 Controller 中看到的那样,我仍然能够一直循环并执行诸如 mothers.First().Family.Fathers 之类的操作.First().Children.First().Mother.Family.Name
没有任何问题。但是我一直在查看的所有关于 EF 和 Code First 建模的教程和示例(例如 this one by Scott Guthrie)都包含这些属性,所以不使用它们感觉不对。
那么,我的第二个问题是:这样做会有什么缺点和问题我还没有发现吗?
在此处下载示例项目:http://blackfin.cannedtuna.org/cyclical-reference-test-app.zip , 然后打开 TestSolution.sln。这些属性在示例项目中被注释掉了。取消注释 TestModels.cs 中添加属性的行,导致循环引用异常。
注意:解决方案是创建位于 c:\TestApp.sdf 的 SQL CE 数据库并为之播种
2011 年 12 月更新:我从来没有从技术上解决过这个问题,但我辞掉了工作,找到了另一份我不必使用微软技术的工作。这样解决了我的问题:)
作为老地方的技术支持,过去在解决问题时会写:“已提供解决方法或解决方案”。
最佳答案
But if I created the database tables myself (which I prefer not to, that's what I like about Code First) it would be a perfectly valid data structure, as far as I can tell.
这是您应该仔细检查的内容。异常直接来自数据库而不是 Entity Framework 。手动创建的具有相同约束的表结构也可能无效。请记住,您的外键属性 Mother.FamilyID
、Father.FamilyID
、Child.MotherID
和 Child.FatherID
是不可为空,因此它们表示所需的关系,并且数据库中的相应列也不可为空。
当您从模型类中删除所有这些属性时,您的关系突然变得可选,因为导航属性可以是null
。这是另一个模型,因为数据库中的 FK 列可以为空!显然这是允许的模型。
如果你想在你的模型中仍然有代表可选关系而不是必需关系的外键属性,你可以使用可为空的类型:public int? FamilyID { 得到;放; }
, 公共(public)整数?母亲ID { 得到;放;
等
关于c# - Entity Framework 、Code First 建模和循环引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6982153/
code
正常吗?
我刚刚开始使用 YARD 来记录我的 Rails 应用程序。我没有指定任何特定的标记处理程序,但我希望 `code` 会转换为 code,但这似乎没有发生。这是正常行为吗?我是否必须添加一些额外的选项
什么是Code-Server 首先程序员朋友们肯定都用过来自微软的VS Code 这款轻量而又高级的编辑器,拥有丰富的插件库,支持各种语言编译运行。而本文介绍的Code-Server就是coder 公
我是一名高中生,今年开始学习汇编。 我目前正在制作 Pacman 克隆作为我的最终项目。 我遇到的唯一问题是我的代码很大,*.exe 文件几乎有 64KB。 所以我的问题是,如果我转向模型介质,我需要
锁定。这个问题及其答案是locked因为这个问题是题外话,但具有历史意义。它目前不接受新的答案或互动。 挑战 按字符计数绘制 Code 39 条码的 ASCII 表示的最短代码。 维基百科关于代码 3
我正在开发 VS 代码的扩展(使用 javascript)。现在我需要安装 VS Code 的路径。 windows有一种方法: var child = require('child_process'
[Windows 10] 我在自定义目录中安装了“Microsoft VS Code(用户设置)”,每当我尝试更新它时,都会显示: 然后这个 Log Info Dec 23 11:42:40.673
我正在尝试更新我的 VS 代码,但收到一条错误消息:由于防病毒软件和/或进程失控,更新可能会失败。 附加了一个来 self 的用户的日志文件,但我不确定要检查什么。我对计算机和编程还是个新手。 最佳答
几天前我安装了 Kali Linux。我正在尝试使用 Code-OSS 而不是 VSCode,因为最新版本的 Kali 没有安装普通版本所需的库。 如果我尝试使用 code-oss . 或 code
我正在从 Atom 迁移到 VS Code,因为这似乎是当今所有酷 child 都在使用的东西。 在 atom 中,我能够如图所示突出显示当前行号(装订线中的蓝色突出显示)。 有没有办法在 VS Co
我试图找到一个明确的 G 代码语法规范,而不是单个 G 代码的含义,我无处不在的规范,我的意思是详细的语法规范,目的是编写解析器。 我编写解析器没有问题,我只是在寻找语法规范,例如。我知道您不必总是为
我想在 VS Code (Windows) 中使用 Fira Code,并且已经按照 instructions 中的说明配置了字体。 。不知何故,字体看起来很模糊。我该如何解决这个问题? "edito
这个问题已经有答案了: How can I navigate back to the last cursor position in Visual Studio Code? (16 个回答) 已关闭
如何选择当前单词,即插入符号所在的位置。 注意:我正在寻找 Visual Studio Code(VS Code)(文本编辑器)的快捷方式,而不是 Visual Studio IDE。 最佳答案 在
我需要在 VS Code 中安装 flutter 但在安装扩展中,我有这个错误 Unable to install 'Dart-Code.flutter'; there is no available
memberData
有什么区别
{@code memberData} 和有什么区别?和 memberData在 JavaDoc 中 最佳答案 有两个主要区别: {@code ...}更简洁:更易于阅读(和输入)。 {@code ..
我有这样一个字符串: Here is my code sample, its not too great: [CODE] [/CODE] I hope you enjoy. 现在我想用 highli
在 VS Code 中,我有一个少于 50 个文件的 Vue 项目,但是在运行开发服务器时 VS Code 抛出 Error: ENOSPC: System limit for number of f
Source Code Pro 如何在 VSC 中使用 ExtraLight ~? 似乎以下不起作用...... 我确定我有字体。 Source Code Pro ExtraLight 最佳答案 编辑
我对 Visual Studio Code 很陌生。我正在尝试调试一个已经存在的应用程序,我已经通过 Git 克隆了它。我的文件都没有被修改。我已经下载了微软扩展“C# for Visual Stud
Visual Code VS Visual Studio Code Insider 我还是不明白这两者有什么区别,难道其中一个是新功能的试用版吗? 最佳答案 Visual Studio Code In
我是一名优秀的程序员,十分优秀!