I have following EF code first code. I am getting the following exception:
<\'GiftCouponPayment\' does not contain an identity column.
You are not specifying the ID field for your TPC / TPT mappings. Even with inheritance you need to do this when not running a TPH mappings. (To note, I'm also not sure on the MapInheritedProperties()
call... This is generally used for TPH... not TPT)
//Fluent API - Table per Concrete Type (TPC)
modelbuilder.Entity<GiftCouponPayment>()
.HasKey(x => x.PaymentComponentID)
.Map(m =>
{
m.MapInheritedProperties();
m.ToTable("GiftCouponPayment");
})
.Property(x => x.PaymentComponentID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
This needs to be on each and every class mapping from the concrete type. If it were me I would go with TPH mapping where GiftCoupon as well as the other inheritance mapping so you end up with 1 table to represent the entire tree of objects using a discriminator column.
Regardless... The other thing you are missing in your base class is:
public byte[] Version { get; set; }
And the associated mapping for:
Property(x => x.Version).IsConcurrencyToken()
Which allows for optimistic concurrency.
Hopefully this helps a bit, let me know if you need further assistance or clarification.
On your PaymentComponent class decorate the ID with KeyAttribute
[Key]
public int PaymentComponentID { get; set; }
I see that my initial advice about TPC wasn't correct because you are also using FK in the base class - do you see the PaymentComponent
table? It should not be there in case of TPC inheritance. Try to use TPT inheritance (remove MapInheritedProperties
from your mapping). This will end with the same correct database. Don't use reseed. The Id will be controlled just by identity column in PaymentComponent
table (as it is at the moment).