【发布时间】:2016-04-18 05:11:17
【问题描述】:
如何配置具有模型类的 EF6 迁移?
- 项目集合
- 一个特定项目的导航属性
public class MyModel
{
[Key]
public int Id { get; set; }
// My collection of elements
public virtual ICollection<MyCollectionElement> MyCollection { get; set; }
// Optional navigation to a particular element from the collection
[ForeignKey("CurrentElement")]
public int? CurrentElementId { get; set; }
public virtual MyCollectionElement CurrentElement { get; set; }
}
public class MyCollectionElement
{
[Key]
public int Id { get; set; }
// Required navigation to MyClass
[ForeignKey("MyModel")]
public int MyModelID { get; set; }
public virtual MyModel Model { get; set; }
}
配置
modelBuilder.Entity<MyModel>()
.HasMany(x => x.MyCollection)
.WithRequired(x => x.Model)
.HasForeignKey(x => x.MyModelID)
.WillCascadadeOnDelete(false);
在Update-Database 上抛出几个错误,比如
无法确定相关操作的有效顺序。
我想要一个不涉及 MyCollectionElement 中的boolean IsCurrent 的解决方案,以便稍后进行另一个查询并查找当前元素;相反,我想将当前元素的 id 与我的模型一起存储,例如暴露。
另外,如果更容易的话,我不介意让 int CurrentElementId 不可为空(必需)。
谢谢。
【问题讨论】:
标签: entity-framework ef-fluent-api