2016-05-14 35 views
0

在这里,我检查数据库获得法术列表,其中ID匹配什么是由用户发送一列:实体框架似乎已经发明了一个不存在

Spell spell = db.Spells.Where(x=>x.Id == spellId).FirstOrDefault(); 

法术类看起来像这样:

public partial class Spell 
{ 
    public int Id { get; set; } 

    [Required] 
    [StringLength(50)] 
    public string Name { get; set; } 

    public string Description { get; set; } 

    [StringLength(50)] 
    public string Page { get; set; } 

    [StringLength(50)] 
    public string Range { get; set; } 

    [StringLength(50)] 
    public string Components { get; set; } 

    public bool? Ritual { get; set; } 

    [StringLength(50)] 
    public string Duration { get; set; } 

    public bool? Concentration { get; set; } 

    [StringLength(50)] 
    public string CastingTime { get; set; } 

    public int Level { get; set; } 

    [StringLength(50)] 
    public string School { get; set; } 

    [Required] 
    [StringLength(100)] 
    public string Classes { get; set; } 

    [StringLength(100)] 
    public string Archetype { get; set; } 

    [StringLength(50)] 
    public string Domains { get; set; } 

    [StringLength(50)] 
    public string Oaths { get; set; } 

    [StringLength(50)] 
    public string Circles { get; set; } 
} 

然而,该行执行的时候,我得到一个错误spellbook_id不是列。

我检查对正在执行的查询,这是它:

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[Name] AS [Name], 
[Extent1].[Description] AS [Description], 
[Extent1].[Page] AS [Page], 
[Extent1].[Range] AS [Range], 
[Extent1].[Components] AS [Components], 
[Extent1].[Ritual] AS [Ritual], 
[Extent1].[Duration] AS [Duration], 
[Extent1].[Concentration] AS [Concentration], 
[Extent1].[CastingTime] AS [CastingTime], 
[Extent1].[Level] AS [Level], 
[Extent1].[School] AS [School], 
[Extent1].[Classes] AS [Classes], 
[Extent1].[Archetype] AS [Archetype], 
[Extent1].[Domains] AS [Domains], 
[Extent1].[Oaths] AS [Oaths], 
[Extent1].[Circles] AS [Circles], 
[Extent1].[Spellbook_Id] AS [Spellbook_Id] 
FROM [dbo].[Spells] AS [Extent1] 

正如你所看到的,不知何故EF请求从表中Spellbook_Id,但它不存在。我不确定它在哪里得到这个想法。我有一个Spellbook类,它只是有一个ID,一个用户ID和List<Spell>,但我试图运行不应该被引用的魔法书在所有的查询,并有上dbo.Spells

没有外键约束

编辑:添加每个请求更多的代码

public class Spellbook 
{ 
    [Key] 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string UserId { get; set; } 
    public List<Spell> Spells { get; set; } 
} 

,这里是我的上下文:

public partial class SpellbookAPIContext : DbContext 
{ 
    public SpellbookAPIContext() : base("name=SpellbookAPIContext") 
    { 
    } 

    public virtual DbSet<Spell> Spells { get; set; } 
    public virtual DbSet<Spellbook> Spellbooks { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Spell>() 
      .Property(e => e.Name) 
      .IsUnicode(false); 

     modelBuilder.Entity<Spell>() 
      .Property(e => e.Description) 
      .IsUnicode(false); 

     modelBuilder.Entity<Spell>() 
      .Property(e => e.Page) 
      .IsUnicode(false); 

     modelBuilder.Entity<Spell>() 
      .Property(e => e.Range) 
      .IsUnicode(false); 

     modelBuilder.Entity<Spell>() 
      .Property(e => e.Components) 
      .IsUnicode(false); 

     modelBuilder.Entity<Spell>() 
      .Property(e => e.Duration) 
      .IsUnicode(false); 

     modelBuilder.Entity<Spell>() 
      .Property(e => e.CastingTime) 
      .IsUnicode(false); 

     modelBuilder.Entity<Spell>() 
      .Property(e => e.School) 
      .IsUnicode(false); 

     modelBuilder.Entity<Spell>() 
      .Property(e => e.Classes) 
      .IsUnicode(false); 

     modelBuilder.Entity<Spell>() 
      .Property(e => e.Archetype) 
      .IsUnicode(false); 

     modelBuilder.Entity<Spell>() 
      .Property(e => e.Domains) 
      .IsUnicode(false); 

     modelBuilder.Entity<Spell>() 
      .Property(e => e.Oaths) 
      .IsUnicode(false); 

     modelBuilder.Entity<Spell>() 
      .Property(e => e.Circles) 
      .IsUnicode(false); 

     modelBuilder.Entity<Spellbook>() 
      .Property(e => e.Id) 
      .IsRequired(); 

     modelBuilder.Entity<Spellbook>() 
      .Property(e => e.Name) 
      .IsRequired(); 

     modelBuilder.Entity<Spellbook>() 
      .Property(e => e.UserId) 
      .IsRequired(); 
    } 
} 
+0

您是否正在使用映射类?另外,你在魔法书类别中引用的列表是法术列表吗? – abrown

+0

这是,我输入了,但我认为它消失了,因为我没有使用反引号。让我更新一下。而且我还没有使用映射类,因为我不需要它 –

+0

因为魔法书具有类型ICollection的导航属性,法术需要魔法书表的外键来告诉哪些魔法属于哪个魔法书。由于EF没有找到FK,但它需要建立关系,因此它发明了它。 – DevilSuichiro

回答

0

我有一个魔法书类,它只是有一个ID,一个用户ID和列表...

那会是一个法术列表,也许?也许实体框架假设你在拼写中有一个外键,因为Spellbook有一个ICollection<Spell>属性。

您可以尝试发布您的Spellbook代码以及您的Entity Framework配置类。实体框架可以通过三种方式进行配置:在你的模型

这可能是因为你在这三个领域之一有一个关系。

了解更多关于Relationships and Navigation Properties

+0

谢谢你的回应。我添加了Spellbook类以及我的整个上下文类。我对EntityFramework很新。事实上,这是我第一次有超过一张桌子。整个项目对我来说是一次巨大的学习经历 –

相关问题