【发布时间】:2013-03-31 19:03:54
【问题描述】:
我最近使用 EF Reverse Engineer Code First 将现有数据库转换为代码。在完善了大部分结果之后,我开始编写测试。我当前遇到的错误是尝试访问名为“Element_ElementCode”的列名,但是不存在这样的列。
为了确定这一点,我对我的整个项目进行了搜索,以避免意外声明它的可能性。
Find all "Element_ElementCode", Subfolders, Find Results 1, Entire Solution, ""
Matching lines: 0 Matching files: 0 Total files searched: 120
具体错误如下:
System.Data.SqlClient.SqlException: Invalid column name 'Element_ElementCode'.
无效的列名“Element_ElementCode”重复了 10-15 次,堆栈跟踪没有提供任何线索。
当执行包含检索数据的表达式并对其执行一些断言的测试时会发生此异常。
var doos = (dbContext.Elementen.Where(d => d.ElementCode == "DOOS9001")).FirstOrDefault();
这是来自 SQL Server 本身的查询的结果:
Element(在Element.cs内)有以下字段:
ElementCode
Doelgroep
Type
Omschrijving
Titel
元素的映射如下:
public class ElementenMap : EntityTypeConfiguration<Element> {
public ElementenMap() {
// Primary Key
this.HasKey(t => t.ElementCode);
// Properties
this.Property(t => t.ElementCode)
.IsRequired()
.HasMaxLength(255);
this.Property(t => t.Type)
.HasMaxLength(31);
this.Property(t => t.Doelgroep)
.HasMaxLength(255);
this.Property(t => t.Omschrijving)
.HasMaxLength(255);
this.Property(t => t.Titel)
.HasMaxLength(255);
// Table & Column Mappings
this.ToTable("Elementen");
this.Property(t => t.ElementCode).HasColumnName("ElementCode");
this.Property(t => t.Type).HasColumnName("Type");
this.Property(t => t.Doelgroep).HasColumnName("Doelgroep");
this.Property(t => t.Omschrijving).HasColumnName("Omschrijving");
this.Property(t => t.Titel).HasColumnName("Titel");
// Relationships
this.HasMany(t => t.Kernwoorden)
.WithRequired(t => t.Element)
.Map(m => m.ToTable("Kernwoorden"));
}
}
此外:我确定该数据库已被访问,因为对同一数据库内不同表的其他测试成功。
我已尝试提供尽可能多的相关信息,如果我忘记了来源,请告诉我。为什么它试图访问名为Element_ElementCode 的列?是否可能是我忘记关闭的约定(我已经不得不关闭 PluralizingTableNameConvention)?
我应该寻找什么方向?
编辑:
Kernwood.cs
public class Kernwoord {
public string ElementCode { get; set; }
public string KernwoordString { get; set; }
public virtual Element Element { get; set; }
}
Element.cs
public partial class Element {
public Element() {
this.LeertrajectElementen = new List<LeertrajectElement>();
this.Kernwoorden = new List<Kernwoord>();
}
public string ElementCode { get; set; }
public string Type { get; set; }
public string Doelgroep { get; set; }
public string Omschrijving { get; set; }
public string Titel { get; set; }
public virtual Casus Casus { get; set; }
public virtual Document Document { get; set; }
public virtual Doos Doos { get; set; }
public virtual ICollection<LeertrajectElement> LeertrajectElementen { get; set; }
public virtual StellingenSpel StellingenSpel { get; set; }
public virtual ICollection<Kernwoord> Kernwoorden { get; set; }
}
LeertrajectElementenMap.cs sn-p:
this.HasRequired(t => t.Element)
.WithMany(t => t.LeertrajectElementen)
.HasForeignKey(d => d.ElementCode);
编辑:
通过修复现有的继承问题解决了问题。
【问题讨论】:
-
为什么要映射
this.Property(t => t.ElementCode).HasColumnName("ElementCode");和.Map(m => m.ToTable("Kernwoorden"));。我建议放弃所有“手动”命名(无论如何都是一样的)并尝试不使用它。你在那里做的“太多”了——你让 EF 感到困惑:) -
没有它没有任何区别,但我已将其调整为类似的(但仍然不是解决方案),如下面的 cmets 中建议的那样。请注意,这是由 EF 自动生成的,它不应该混淆自己!
-
拥有一个我们可以快速测试的完整模型会更容易
-
pastebin.com/DgVws3yX 我相信这些都是使示例工作所需的所有类。
标签: c# sql entity-framework