我遇到了Fluent和Entity Framework的问题。代码首先无法解析表名。坚持表名是复数
首先,我创建了一个新的数据库项目,并使用反向工程师代码优先来生成一组模型和地图。
如果我查看现有数据库中的表,我有一个名为Parcel的表和一个名为ParcelAgremment的表。
当我看映射的两个表我看到:
public class ParcelMap : EntityTypeConfiguration<Parcel>
{
public ParcelMap()
{
// Primary Key
this.HasKey(t => t.ParcelId);
// Properties
this.Property(t => t.ParcelReference)
.IsRequired()
.HasMaxLength(20);
this.Property(t => t.ParcelDescription)
.HasMaxLength(1000);
// Table & Column Mappings
this.ToTable("Parcel");
this.Property(t => t.ParcelId).HasColumnName("ParcelId");
this.Property(t => t.SiteId).HasColumnName("SiteId");
this.Property(t => t.ParentParcelId).HasColumnName("ParentParcelId");
this.Property(t => t.IsCurrent).HasColumnName("IsCurrent");
this.Property(t => t.ParcelTypeId).HasColumnName("ParcelTypeId");
this.Property(t => t.ParcelReference).HasColumnName("ParcelReference");
this.Property(t => t.ParcelDescription).HasColumnName("ParcelDescription");
// Relationships
this.HasOptional(t => t.Parcel2)
.WithMany(t => t.Parcel1)
.HasForeignKey(d => d.ParentParcelId);
this.HasRequired(t => t.ParcelType)
.WithMany(t => t.Parcels)
.HasForeignKey(d => d.ParcelTypeId);
this.HasRequired(t => t.Site)
.WithMany(t => t.Parcels)
.HasForeignKey(d => d.SiteId);
}
}
public class ParcelAgreementMap : EntityTypeConfiguration<ParcelAgreement>
{
public ParcelAgreementMap()
{
// Primary Key
this.HasKey(t => t.ParcelAgreementId);
// Properties
// Table & Column Mappings
this.ToTable("ParcelAgreement");
this.Property(t => t.ParcelAgreementId).HasColumnName("ParcelAgreementId");
this.Property(t => t.ParcelId).HasColumnName("ParcelId");
this.Property(t => t.AgreementTypeId).HasColumnName("AgreementTypeId");
this.Property(t => t.RightsChecked).HasColumnName("RightsChecked");
this.Property(t => t.OptionRightsTypeId).HasColumnName("OptionRightsTypeId");
this.Property(t => t.AgreementDate).HasColumnName("AgreementDate");
this.Property(t => t.AgreementNotes).HasColumnName("AgreementNotes");
this.Property(t => t.ConditionsChecked).HasColumnName("ConditionsChecked");
this.Property(t => t.IsAssignable).HasColumnName("IsAssignable");
// Relationships
this.HasOptional(t => t.OptionRightsType).WithMany(t => t.ParcelAgreements).HasForeignKey(d => d.OptionRightsTypeId);
this.HasRequired(t => t.AgreementType).WithMany(t => t.ParcelAgreements).HasForeignKey(d => d.AgreementTypeId);
this.HasRequired(t => t.Parcel).WithMany(t => t.ParcelAgreements).HasForeignKey(d => d.ParcelId);
}
}
和模型文件是:
public class Parcel : Entity
{
public Parcel()
{
this.LandTransactionElements = new List<LandTransactionElement>();
this.Parcel1 = new List<Parcel>();
this.ParcelAgreements = new List<ParcelAgreement>();
this.ParcelDeedPackets = new List<ParcelDeedPacket>();
this.ParcelExceptions = new List<ParcelException>();
this.ParcelFinancialTemplates = new List<ParcelFinancialTemplate>();
this.ParcelNote = new List<ParcelNote>();
this.ParcelOptions = new List<ParcelOption>();
this.ParcelReferences = new List<ParcelReference>();
this.ParcelRentPayments = new List<ParcelRentPayment>();
this.ParcelRights = new List<ParcelRight>();
this.ParcelStampDuties = new List<ParcelStampDuty>();
this.ParcelVolumes = new List<ParcelVolume>();
this.ReviewPlans = new List<ReviewPlan>();
}
public int ParcelId { get; set; }
public int SiteId { get; set; }
public Nullable<int> ParentParcelId { get; set; }
public bool IsCurrent { get; set; }
public int ParcelTypeId { get; set; }
public string ParcelReference { get; set; }
public string ParcelDescription { get; set; }
public virtual ICollection<LandTransactionElement> LandTransactionElements { get; set; }
public virtual ICollection<Parcel> Parcel1 { get; set; }
public virtual Parcel Parcel2 { get; set; }
public virtual ParcelType ParcelType { get; set; }
public virtual Site Site { get; set; }
public virtual ICollection<ParcelAgreement> ParcelAgreements { get; set; }
public virtual ICollection<ParcelDeedPacket> ParcelDeedPackets { get; set; }
public virtual ICollection<ParcelException> ParcelExceptions { get; set; }
public virtual ICollection<ParcelFinancialTemplate> ParcelFinancialTemplates { get; set; }
public virtual ICollection<ParcelNote> ParcelNote { get; set; }
public virtual ICollection<ParcelOption> ParcelOptions { get; set; }
public virtual ICollection<ParcelReference> ParcelReferences { get; set; }
public virtual ICollection<ParcelRentPayment> ParcelRentPayments { get; set; }
public virtual ICollection<ParcelRight> ParcelRights { get; set; }
public virtual ICollection<ParcelStampDuty> ParcelStampDuties { get; set; }
public virtual ICollection<ParcelVolume> ParcelVolumes { get; set; }
public virtual ICollection<ReviewPlan> ReviewPlans { get; set; }
}
}
public class ParcelAgreement : Entity
{
public int ParcelAgreementId { get; set; }
public int ParcelId { get; set; }
public int AgreementTypeId { get; set; }
public bool RightsChecked { get; set; }
public Nullable<int> OptionRightsTypeId { get; set; }
public Nullable<System.DateTime> AgreementDate { get; set; }
public string AgreementNotes { get; set; }
public Nullable<bool> ConditionsChecked { get; set; }
public Nullable<bool> IsAssignable { get; set; }
public virtual AgreementType AgreementType { get; set; }
public virtual OptionRightsType OptionRightsType { get; set; }
public virtual Parcel Parcel { get; set; }
}
我打电话使用
var retData = parcelRepository
.Query(s=> s.ParcelId == optionId)
.Select()
.ToList();
数据库该查询起作用,但是当我尝试从Parecl对象访问ParcelAgreement对象时,出现以下错误
InnerException {"Invalid object name 'dbo.ParcelAgreements'."} System.Exception {System.Data.SqlClient.SqlException}
该表在数据库中称为ParcelAgreement not ParcelAgreements。
如果使用ParcelAgreements的全局搜索检查项目,它将不会返回任何结果。
我有其他表链接,它也没有复数表名,它正在恢复数据正常。
根据要求:更新
在我的上下文文件。
public DbSet<Parcel> Parcels { get; set; }
public DbSet<ParcelAgreement> ParcelAgreements { get; set; }
并在OnModelCreating子。
modelBuilder.Configurations.Add(new ParcelMap());
modelBuilder.Configurations.Add(new ParcelAgreementMap());
更新5月11日
四处寻找它之后出现的解决方法是
modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
添加到OnModelCreating。按照Link to SO article
unfortunatley这也行不通。
我的代码看起来像
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Configurations.Add(new ActivityMap());
...
... <List of other table>
}
上运行我仍然得到错误:
{"Invalid object name 'dbo.ActivityMaps'."} System.Exception {System.Data.SqlClient.SqlException}
正如你可以看到它仍然想变复数的表名。
我正在使用EF 6.0.0。0
一切看起来不错。你可以验证'ParcelAgreementMap'是否有效(添加到'modelBuilder.Configurations')?因为它里面的所有东西都是按照约定的,除了表名。 –
Ivan,更新了问题 – gilesrpa
我看到了,但无法重现 - 尝试了您的模型和配置(当然注释掉了缺少的部分),并且按预期工作。 –