实体框架似乎发明了一个不存在的专栏

时间:2016-05-14 19:08:53

标签: c# entity-framework

在这里,我正在检查数据库以获取一个符号列表,其中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>,但我尝试运行的查询根本不应该引用Spellbook,并且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();
    }
}

1 个答案:

答案 0 :(得分:0)

  

我有一个Spellbook类,它只有id,userid和List ......

这可能是法术列表吗?也许Entity Framework假设您在Spell中有一个外键,因为Spellbook具有ICollection<Spell>属性。

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

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

详细了解Relationships and Navigation Properties