在這裏,我檢查數據庫獲得法術列表,其中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();
}
}
您是否正在使用映射類?另外,你在魔法書類別中引用的列表是法術列表嗎? – abrown
這是,我輸入了,但我認爲它消失了,因爲我沒有使用反引號。讓我更新一下。而且我還沒有使用映射類,因爲我不需要它 –
因爲魔法書具有類型ICollection的導航屬性,法術需要魔法書表的外鍵來告訴哪些魔法屬於哪個魔法書。由於EF沒有找到FK,但它需要建立關係,因此它發明了它。 –
DevilSuichiro