Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/NickStrupat/EntityFramework.PrimaryKey
Retrieve the primary key (including composite keys) from any entity
https://github.com/NickStrupat/EntityFramework.PrimaryKey
Last synced: 12 days ago
JSON representation
Retrieve the primary key (including composite keys) from any entity
- Host: GitHub
- URL: https://github.com/NickStrupat/EntityFramework.PrimaryKey
- Owner: NickStrupat
- License: mit
- Created: 2015-12-17T05:17:53.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2020-06-25T13:07:11.000Z (over 4 years ago)
- Last Synced: 2024-10-12T17:43:15.267Z (27 days ago)
- Language: C#
- Size: 59.6 KB
- Stars: 44
- Watchers: 6
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-dotnet-core - EntityFramework.PrimaryKey - Easily get the primary key of any entity (including composite keys). (Frameworks, Libraries and Tools / ORM)
- awesome-dotnet-core - EntityFramework.PrimaryKey - EF的主键库。 (框架, 库和工具 / 对象关系映射ORM)
- fucking-awesome-dotnet-core - EntityFramework.PrimaryKey - Easily get the primary key of any entity (including composite keys). (Frameworks, Libraries and Tools / ORM)
- awesome-dotnet-core - EntityFramework.PrimaryKey - Easily get the primary key of any entity (including composite keys). (Frameworks, Libraries and Tools / ORM)
README
# EntityFramework.PrimaryKey
Retrieve the primary key (including composite keys) from any entity as a dictionary.| EF version | .NET support | NuGet package |
|:-----------|:------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 6.1.3 | >= Framework 4.6.1 | [![NuGet Status](http://img.shields.io/nuget/v/EntityFramework.PrimaryKey.svg?style=flat)](https://www.nuget.org/packages/EntityFramework.PrimaryKey/) |
| Core 2.0 | >= Framework 4.6.1 || >= Standard 2.0 | [![NuGet Status](http://img.shields.io/nuget/v/EntityFrameworkCore.PrimaryKey.svg?style=flat)](https://www.nuget.org/packages/EntityFrameworkCore.PrimaryKey/) |## Usage
```csharp
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)];
```## API
```csharp
var primaryKeyDict = context.GetPrimaryKey(entity);
var primaryKeyDict = entity.GetPrimaryKey(); // auto-detects your DbContext class
var primaryKeyDict = entity.GetPrimaryKey(context);
var primaryKeyDict = entity.GetPrimaryKey();
```## How it works
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` 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.
## Contributing
1. [Create an issue](https://github.com/NickStrupat/EntityFramework.PrimaryKey/issues/new)
2. Let's find some point of agreement on your suggestion.
3. Fork it!
4. Create your feature branch: `git checkout -b my-new-feature`
5. Commit your changes: `git commit -am 'Add some feature'`
6. Push to the branch: `git push origin my-new-feature`
7. Submit a pull request :D## History
[Commit history](https://github.com/NickStrupat/EntityFramework.PrimaryKey/commits/master)
## License
[MIT License](https://github.com/NickStrupat/EntityFramework.PrimaryKey/blob/master/README.md)