0

Ok, I have this configuration setting up an Identifying Relationship between an OrderPage and its collection of child OrderPageShipDate instances:

public class OrderPageShipDateConfiguration : EntityTypeConfiguration<OrderPageShipDate>
{
    public OrderPageShipDateConfiguration()
    {
        this.HasKey(t => new { t.OrderPageId, t.Id });
        this.Property(p => p.OrderPageId).HasColumnOrder(1);
        this.Property(p => p.Id).HasColumnOrder(2)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        this.HasRequired(opi => opi.OrderPage)
            .WithMany(op => op.ShipDates)
            .HasForeignKey(opi => opi.OrderPageId);
    }

With these participants:

/// <summary>
/// This class represents a a set of items added to an order from a particular bulletin at a particular time.
/// </summary>
public class OrderPage : IEntity, IAuditInfo
{       
    #region Construction
    //...
    #endregion

    public int Id { get; set; }

    // other properties...

    public virtual ICollection<OrderPageShipDate> ShipDates { get; set; }
}

_

/// <summary>
/// This class represents a shipping date for an order page that has been added to an order.
/// </summary>
public class OrderPageShipDate : IEntity
{
    #region Construction
    // ...
    #endregion

    public int Id { get; set; }

    public int OrderPageId { get; set; }

    public virtual OrderPage OrderPage { get; set; }

    public DateTime ShipDate { get; set; }
}

I am not getting the Identity Specification set on the Id column of the OrderPageShipDate table.

Any idea why?

Could it have anything to do with the Id properties being specified in an IEntity interface that these classes implement?

I have tried both the Data Annotation approach and the Fluent API approach as you see here.

crthompson
  • 15,653
  • 6
  • 58
  • 80
jlavallet
  • 1,267
  • 1
  • 12
  • 33

2 Answers2

1

By convention, EF should pick up the Id as key, and put identity attribute on it. I think this.HasKey(t => new { t.OrderPageId, t.Id }) may be the issue, do you want a composite key here?

J.W.
  • 17,991
  • 7
  • 43
  • 76
  • I think that's the likely culprit here. Just be aware that you need to drop and recreate this table to get it to change the column and EF won't scaffold that for you in a migration. http://stackoverflow.com/a/18917348/150342 – Colin Sep 25 '13 at 01:15
  • Hi guys, I'm using the composite key to set up an Identifying Relationship as per the EF documentation and exemplified here: [Proper implementation of an Identifying relationship in Entity Framework?](http://stackoverflow.com/questions/9579602/proper-implementation-of-an-identifying-relationship-in-entity-framework) – jlavallet Sep 25 '13 at 13:44
0

For the immediate problem that the identity specifications were not being created in the database, I have to give Collin credit. He put me on the right path to drop the database tables. Since I'm in development, I just dropped the whole database and let it re-create itself. I was using database migrations and I did not realize that it would not do schema changes like that.

jlavallet
  • 1,267
  • 1
  • 12
  • 33