我遇到了Fluent和Entity Framework的问题。
首先,我创建了一个新的数据库项目,并使用Reverse Engineer Code First生成一组模型和地图。
如果我查看现有数据库中的表,我有一个名为Parcel的表和一个名为ParcelAgremment的表。
当我查看两个表的映射时,我看到:
www.domainname.com/static/style.css
,模型文件为:
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; }
}
查询有效,但是当我尝试从Parecl对象访问ParcelAgreement对象时出现以下错误
var retData = parcelRepository
.Query(s=> s.ParcelId == optionId)
.Select()
.ToList();
该表在数据库中称为ParcelAgreement而不是ParcelAgreements。
如果使用全局搜索ParcelAgreements检查项目,则返回任何未找到的内容。
我有其他链接的表,它们也没有多个表名,并且正在恢复数据。
根据要求:更新
在我的上下文文件中。
InnerException {"Invalid object name 'dbo.ParcelAgreements'."} System.Exception {System.Data.SqlClient.SqlException}
在OnModelCreating sub。
中public DbSet<Parcel> Parcels { get; set; }
public DbSet<ParcelAgreement> ParcelAgreements { get; set; }
5月11日更新
环顾四周之后,似乎是要添加
modelBuilder.Configurations.Add(new ParcelMap());
modelBuilder.Configurations.Add(new ParcelAgreementMap());
到OnModelCreating。根据{{3}}
不幸的是,这也行不通。我的代码看起来像
modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
在跑步时我仍然收到错误:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Configurations.Add(new ActivityMap());
...
... <List of other table>
}
正如您所见,它仍在尝试复数表名。
我使用的是EF 6.0.0.0
答案 0 :(得分:0)
这就是我解决它的方式:modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();