Click here to Skip to main content
15,741,391 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
How can I log Identity tables?
I logged all tables but for Identity, tables have some problems.
I could not log it; In change tracking process It throws an error about primary keys

public class ApplicationRoleMetaData : IdentityRole<int, ApplicationUserRoleMetaData>

I've Inherited from
IdentityRole


My question is How could I solve this error.

What I have tried:

I have implemented a log for tables but it throws an error in primary keys in a table.

Edit: From comment:
this is Error Message
Sequence contains no matching element
this line raised noted error
C#
string keyName = dbEntry.Entity.GetType().GetProperties().Single(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0).Name;
Posted
Updated 5-Oct-20 4:45am
v2
Comments
Richard MacCutchan 4-Oct-20 6:32am    
"it throws an error"
This is one of the most useless messages we see here. We have no idea what your code is doing, what the error message is, or what other problem(s) you may be seeing.
Moses_k 4-Oct-20 7:11am    
this is Error Message => Sequence contains no matching element

this line raised noted error =>
string keyName = dbEntry.Entity.GetType().GetProperties().Single(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0).Name;

If you said: Give me an apple, and I said: "I have none" ... Is that an "error"?

You ran a query that returned no results. Period.
 
Share this answer
 
Primary key properties don't necessarily have the [Key] attribute applied. They could be configured via the fluent API, or by convention.

Also remember that entities can have composite primary keys, where the key consists of more than one property.

Extracting the primary key properties is fairly easy:

Entity Framework 6:
C#
private static IReadOnlyList<string> GetPrimaryKeyProperties(this IObjectContextAdapter context, Type entityType)
{
    if (!context.ObjectContext.MetadataWorkspace.TryGetItem(entityType.FullName, DataSpace.OSpace, out EntityType dataType)) return Array.Empty<string>();
    return dataType.KeyProperties.Select(p => p.Name).ToList();
}

public static IReadOnlyDictionary<string, DbPropertyEntry> ExtractPrimaryKey<TEntity>(this DbContext context, DbEntityEntry<TEntity> entry) where TEntity : class
{
    var result = new Dictionary<string, DbPropertyEntry>(StringComparison.Ordinal);
    foreach (string name in context.GetPrimaryKeyProperties(typeof(TEntity)))
    {
        result.Add(name, entry.Property(name));
    }
    
    return result;
}

Entity Framework Core:
C#
public static IReadOnlyDictionary<string, PropertyEntry> ExtractPrimaryKey<TEntity>(this DbContext context, EntityEntry<TEntity> entry) where TEntity : class
{
    IKey primaryKey = entry.Metadata.FindPrimaryKey();
    return primaryKey.Properties.ToDictionary(p => p.Name, p => entry.Property(p.Name), StringComparer.Ordinal);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900