Skip to content
HenkKin edited this page Oct 2, 2019 · 4 revisions

Identifiers.EntityFrameworkCore.SqlServer

Build Status NuGet NuGet BCH compliance

Summary

The Identifiers.EntityFrameworkCore.SqlServer library is an extension on Identifiers.

This library is Cross-platform, supporting netstandard2.1.

Installing Identifiers.EntityFrameworkCore.SqlServer

You should install Identifiers.EntityFrameworkCore.SqlServer with NuGet:

Install-Package Identifiers.EntityFrameworkCore.SqlServer

Or via the .NET Core command line interface:

dotnet add package Identifiers.EntityFrameworkCore.SqlServer

Either commands, from Package Manager Console or .NET Core CLI, will download and install Identifiers.EntityFrameworkCore.SqlServer and all required dependencies.

Dependencies

Usage

IIf you're using EntityFrameworkCore and you want to use this Identifier type in your entities, then you can use Identifiers.EntityFrameworkCore.SqlServer package which includes a DbContextOptionsBuilder.UseIdentifiers<[InternalClrType:short|int|long|Guid]>() extension method, allowing you to register all needed IValueConverterSelectors and IMigrationsAnnotationProviders. It also includes a PropertyBuilder<Identifier>.IdentifierValueGeneratedOnAdd() extension method, allowing you to register all needed configuration to use SqlServerValueGenerationStrategy.IdentityColumn.

To use it:

...
using Identifiers.EntityFrameworkCore.SqlServer;

public class Startup
{
    ...
    
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        ...
         services.AddDbContextPool<YourDbContext>((serviceProvider, options) =>
                    options.UseIdentifiers<short|int|long|Guid>()
                );
                
         // or
                
         services.AddDbContext<YourDbContext>((serviceProvider, options) =>
                    options.UseIdentifiers<short|int|long|Guid>()
                );
        ...
    }
    
    ...

Using the PropertyBuilder:

...
using Identifiers.EntityFrameworkCore.SqlServer;

public class YourDbContext : DbContext
{
    ...
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        ...
        modelBuilder.Entity<YourEntity>()
            .Property(e => e.YourIdProperty)
            .IdentifierValueGeneratedOnAdd();
    }
    ...