1

I have two tables

  • StoreOrderItem
  • StoreOrder

Now StoreOrder has many StoreOrderItems and StoreOrderItem has one StoreOrder (simple 1 to many relationship)

public class StoreOrderItemMap : EntityTypeConfiguration<StoreOrderItem>
{
        public StoreOrderItemMap()
        {
            this.ToTable("StoreOrderItem");
            this.HasKey(op => op.Id);
            this.Property(op => op.StoreOrderId).HasColumnName("order_id");
            ...

            this.HasRequired(so => so.StoreOrder)
              .WithMany(soi => soi.StoreOrderItems)
              .HasForeignKey(so => so.StoreOrderId);
        }
}

public class StoreOrderMap : EntityTypeConfiguration<StoreOrder>
{
    public StoreOrderMap()
    {
        this.ToTable("StoreOrder");
        this.HasKey(op => op.Id);
        ....
    }
}

public class StoreOrderItem
{
    ....
    public virtual int StoreOrderId { get; set; }
    ....

    public virtual StoreOrder StoreOrder { get; set; }
}

public class StoreOrder
{
    .... 
    private ICollection<StoreOrderItem> _storeOrderItems;
    ....

    public virtual ICollection<StoreOrderItem> StoreOrderItems
    {
        get { return _storeOrderItems ?? (_storeOrderItems = new List<StoreOrderItem>()); }
        set { _storeOrderItems = value; }
    }

}

//The code which is used to add the configurations to the modelBuilder.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    System.Type configType = typeof(ContentMap);   //any of your configuration classes here
    var typesToRegister = Assembly.GetAssembly(configType).GetTypes()
        .Where(type => !String.IsNullOrEmpty(type.Namespace))
        .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
        foreach (var type in typesToRegister)
        {
            dynamic configurationInstance = Activator.CreateInstance(type);
            modelBuilder.Configurations.Add(configurationInstance);
        }
        base.OnModelCreating(modelBuilder);
}

Note I am running this code on an existing DB, how do I tell EF to not select "StoreOrder_Id" and to instead use the existing column "order_id"?

Here is the error:

Server Error in '/' Application.

Invalid column name 'StoreOrder_Id'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'StoreOrder_Id'.

9
  • Are you sure that your map is used in mapping? Commented Oct 5, 2012 at 7:01
  • I'm sorry I don't understand what you mean... Commented Oct 5, 2012 at 7:02
  • I ask if you have added StoreOrderItemMap to DbModelBuilder's Configurations collection when building your model? Commented Oct 5, 2012 at 7:05
  • In your DbContext class did you have method protected override void OnModelCreating(DbModelBuilder modelBuilder)? Commented Oct 5, 2012 at 7:07
  • Yes this is being done for every typeof(EntityTypeConfiguration) using reflection Commented Oct 5, 2012 at 7:07

1 Answer 1

1

Found the problem after a few days of mind bending ......

There was another property in the StoreOrder class

public virtual IList<StoreOrderItem> NonVoucherOrderItems
{
    get
    {
        return this.StoreOrderItems.Where(x => !x.IsVoucher()).ToList();
    }
}

This was being evaluated too early and causing all sorts of chaos, changed to

public virtual IEnumerable<StoreOrderItem> NonVoucherOrderItems
{
    get
    {
        return this.StoreOrderItems.Where(x => !x.IsVoucher());
    }
}

And it worked immediately!!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.