0

I am having a problem with Fluent and Entity Framework.

To start, I created a new database project and used Reverse Engineer Code First to generate a set of Models and Maps.

If I look at the tables in the existing database, I have a table called Parcel and a table called ParcelAgremment.

When I look at the mappings for the two tables I see:

 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);

        }
    }

and the model files are:

 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; }
    }

I am calling the database using

var retData = parcelRepository
                            .Query(s=> s.ParcelId == optionId)
                            .Select()
                            .ToList();

The query works, but when I try to access the ParcelAgreement object from the Parecl object I get following error

    InnerException  {"Invalid object name 'dbo.ParcelAgreements'."} System.Exception {System.Data.SqlClient.SqlException}

The table is called ParcelAgreement not ParcelAgreements in the database.

If check the project by using a global search for ParcelAgreements, it returns nothing found.

I have other tables that are linked which also does not have a plural table name and it is recovering the data fine.

As per request: Update

In my context file.

public DbSet<Parcel> Parcels { get; set; }
public DbSet<ParcelAgreement> ParcelAgreements { get; set; }

And in the OnModelCreating sub.

 modelBuilder.Configurations.Add(new ParcelMap());
 modelBuilder.Configurations.Add(new ParcelAgreementMap());

Update 11th May

After looking around it appears that the fix is to add

modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();

to OnModelCreating. As per Link to SO article

unfortunatley this also does not work.

My code looks like

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    modelBuilder.Configurations.Add(new ActivityMap());
    ...
    ... <List of other table>
}

On running I still get the error:

{"Invalid object name 'dbo.ActivityMaps'."} System.Exception {System.Data.SqlClient.SqlException}

As you can see it is still trying to pluralize the table name.

I am using EF 6.0.0.0

Community
  • 1
  • 1
gilesrpa
  • 969
  • 1
  • 12
  • 35

1 Answers1

0

This is how I solved it: modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();

Steve's a D
  • 3,801
  • 10
  • 39
  • 60
gilesrpa
  • 969
  • 1
  • 12
  • 35