[英]Entity Framework seems to have invented a column that doesn't exist
在這里,我正在檢查數據庫以獲取其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上沒有外鍵約束。法術
編輯:每個請求添加了更多代碼
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();
}
}
我確實有一個Spellbook類,其中只有一個ID,一個用戶ID和一個列表...
也許那是一個法術列表? 也許實體框架假設您在Spell中具有外鍵,因為Spellbook具有ICollection<Spell>
屬性。
您可以嘗試發布您的Spellbook代碼以及Entity Framework配置類。 可以通過三種方式配置實體框架:
您可能在這三個領域之一中有關系。
閱讀有關關系和導航屬性的更多信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.