I have a Class where I have 2 Keys (Composite Keys), then I have my audit log function where I used to get the primary key of an entity like this:
string keyName = dbEntry.Entity.GetType().GetProperties().Single(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0).Name;
The problem is that one of the models im trying to save have a composite key:
[Key,Column("paymentdetailid", TypeName = "int", Order=0)]
public Int32 PaymentDetailId { get; set; }
[Key,Column("chargedetailid", TypeName = "int", Order=1)]
public Int32 ChargeDetailId { get; set; }
Im getting the following error when trying to get the keyName:
Sequence contains more than one matching element
Any clue on how to solve this? I just want to get the first key.
Thanks,
SOLUTION
The solution is this one:
var keyNames = dbEntry.Entity.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0).ToList();
string keyName = keyNames[0].Name;