Retrieve the primary key (including composite keys) from any entity as a dictionary.
public class QuestionTag { [Key, Column(Order = 0)] public Guid QuestionId { get; set; } [Key, Column(Order = 1)] public Int64 TagId { get; set; } public DateTime Inserted { get; set; } } var primaryKey = questionTag.GetPrimaryKey(); var questionId = (Guid) primaryKey[nameof(QuestionTag.QuestionId)]; var tagId = (Int64) primaryKey[nameof(QuestionTag.TagId)];
var primaryKeyDict = context.GetPrimaryKey(entity); var primaryKeyDict = entity.GetPrimaryKey(); // auto-detects your DbContext class var primaryKeyDict = entity.GetPrimaryKey(context); var primaryKeyDict = entity.GetPrimaryKey<YourDbContext, YourEntity>();
The extension methods provide four different ways to get the primary key, but internally they run mostly the same code. The names of the properties are retrieved from the DbContext metadata, and a fast delegate is cached for each entity type when that type is first used. When the fast delegate is invoked with an entity, it creates a ReadOnlyDictionary<K, V>
where the keys are the names of the properties which comprise that entity's primary key and the values are the value of each corresponding property.
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4