2017-05-09 35 views
-1

我遇到了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

+0

一切看起來不錯。你可以驗證'ParcelAgreementMap'是否有效(添加到'modelBuilder.Configurations')?因爲它裏面的所有東西都是按照約定的,除了表名。 –

+0

Ivan,更新了問題 – gilesrpa

+0

我看到了,但無法重現 - 嘗試了您的模型和配置(當然註釋掉了缺少的部分),並且按預期工作。 –

回答

0

這就是我如何解決它:modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();

+0

你應該把你的解決方案在這裏。或者刪除它,因爲它不是我的答案 –

+0

我開始了一個新的類庫項目,並使用代碼優先的方法顛倒了數據庫,現在開始工作。 – gilesrpa