2017-08-14 106 views
0

EF6的新手。我有几个加载得很好的类,但其中一个不是很好。代码优先EF6未取回班级

这是有问题的类

public class Family 
{ 
    public int FamilyId { get; set; } 
    public string Name { get; set; } 
    public string Standing { get; set; } 

    public virtual Person Father { get; set; } 

    public virtual Person Mother { get; set; } 

    public virtual Person Spouse { get; set; } 
    public virtual ICollection<Person> Siblings { get; set; } 
    public virtual ICollection<Person> Children { get; set; } 
} 

它的父类

public class Ven 
{ 
    public int VenId { get; set; } 
    ... 
    public virtual Family Family { get; set; } 
    public virtual ICollection<Devotion> Devotions { get; set; } 
    ... 
} 

而其他的一个工作

public class Devotion 
{ 
    public int DevotionId { get; set; } 
    public string Name { get; set; } 
    public int Rank { get; set; } 
} 

我与种子填充他们过去很简单,但已经爆炸了,因为我一直在试图弄清楚发生了什么。

context.Families.AddOrUpdate(
    f => f.FamilyId, 
    new Family 
    { 
     FamilyId = 1, 
     Name = "Tal", 
     Standing = "Baron", 
     Father = context.Persons.Find(1), 
     Mother = context.Persons.Find(2), 
     Children = new List<Person> {context.Persons.Find(3)} 
    } 
); 

... 
Family = context.Families.Find(1) 
... 
Devotions = new List<Devotion> { context.Devotions.Find(1), context.Devotions.Find(2) }, 

我读过几篇SO帖子,试图找出这一点,但找不到任何帮助。

编辑1: 好吧,所以我尝试了一些东西。我完全删除了数据库并重新启动它。我仍然有同样的问题。

我已验证家庭已添加到表格中。通过抛出错误,我已证实该家庭正在通过身份证找到。

但是Ven表仍然没有在它的Family_FamilyId列中获得值,即使家庭被分配给它。

任何帮助将不胜感激。 DataBase

编辑2: Entire Seed Function。这曾经只是一个大的“新Ven”。

+0

那么你有什么问题!? – Kahbazi

+0

EF6未按标题中所述检索课程的数据。 –

+0

哪个类,哪个变量,哪一行? – Kahbazi

回答

0

好的,我真的不知道为什么,但把Seed放入初始化程序,并将[new Ven]语句放回到一个巨大的add中,使它重新开始工作。