EfCore.Conventions 1.0.0

Setting EF Core by convention

Install-Package EfCore.Conventions -Version 1.0.0
dotnet add package EfCore.Conventions --version 1.0.0
<PackageReference Include="EfCore.Conventions" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EfCore.Conventions --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Icon

EfCore.Conventions

Setting EF Core by conventions

Get it

PM> Install-Package EfCore.Conventions

Setting table names

For example, if you would like to pluralize all table names.

First, you need to get Inflector.

PM> Install-Package Inflector.NetStandard

Then add this to your data context file.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    var inflector = new Inflector.Inflector(CultureInfo.GetCultureInfo("en-US"));
    modelBuilder.WithTableName(type => inflector.Pluralize(type.ClrType.Name));
}

Setting default column types

For example, if you would like to set all decimal to decimal(28, 2).

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.SetDefaultTypeNameForType<decimal>("decimal(28, 2)");
}

Custom action based on property type

For example, if you would like to set Conversion.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ForPropertyType<Employee>(builder => builder.HasConversion(new JsonConverter<Employee>()));
}

Custom action based on a predicate

For example, if you would like to set Conversion to all types annotated with NotMappedAttribute.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ForProperty(
        prop => prop.PropertyType.GetCustomAttribute<NotMappedAttribute>() != null,
        (builder, prop) => builder.HasConversion((ValueConverter)Activator.CreateInstance(typeof(JsonConverter<>).MakeGenericType(prop.PropertyType))));
}

Extended Attributes

EfCore.Conventions also comes with following extended attributes. If you would like to apply the attributes, you need to first set your modelBuilder.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.WithExtendedAttributes();
}
CompositeKey

If you would like to add composite key, you can annotate with [CompositeKey].

For example.

public class OrderItem
{
    [CompositeKey]
    public string OrderId { get; set; }

    [CompositeKey]
    public int RunningNo { get; set; }

    ...
}

You can also annotated with [Column(Order = N)] to set column order.

Index

If you would like to set index to column, you can annotate with [Index].

For example,

public class User
{
    [Key]
    public string Id { get; set; }

    [Index]
    public string Email { get; set; }

    ...
}

You can also set composite and/or unique index.

public class Location
{
    [Index(Name = "IX_Zone_Area", Order = 1, IsUnique = true)]
    public string Zone { get; set; }

    [Index(Name = "IX_Zone_Area", Order = 2, IsUnique = true)]
    public string Area { get; set; }

    ...
}
OnDelete

You can set delete policy on column.

public class OrderItem
{
    public string SkuId { get; set; }

    [OnDelete(DeleteBehavior.Restrict)]
    public Sku Sku { get; set; }

    ...
}

Icon

EfCore.Conventions

Setting EF Core by conventions

Get it

PM> Install-Package EfCore.Conventions

Setting table names

For example, if you would like to pluralize all table names.

First, you need to get Inflector.

PM> Install-Package Inflector.NetStandard

Then add this to your data context file.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    var inflector = new Inflector.Inflector(CultureInfo.GetCultureInfo("en-US"));
    modelBuilder.WithTableName(type => inflector.Pluralize(type.ClrType.Name));
}

Setting default column types

For example, if you would like to set all decimal to decimal(28, 2).

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.SetDefaultTypeNameForType<decimal>("decimal(28, 2)");
}

Custom action based on property type

For example, if you would like to set Conversion.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ForPropertyType<Employee>(builder => builder.HasConversion(new JsonConverter<Employee>()));
}

Custom action based on a predicate

For example, if you would like to set Conversion to all types annotated with NotMappedAttribute.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ForProperty(
        prop => prop.PropertyType.GetCustomAttribute<NotMappedAttribute>() != null,
        (builder, prop) => builder.HasConversion((ValueConverter)Activator.CreateInstance(typeof(JsonConverter<>).MakeGenericType(prop.PropertyType))));
}

Extended Attributes

EfCore.Conventions also comes with following extended attributes. If you would like to apply the attributes, you need to first set your modelBuilder.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.WithExtendedAttributes();
}
CompositeKey

If you would like to add composite key, you can annotate with [CompositeKey].

For example.

public class OrderItem
{
    [CompositeKey]
    public string OrderId { get; set; }

    [CompositeKey]
    public int RunningNo { get; set; }

    ...
}

You can also annotated with [Column(Order = N)] to set column order.

Index

If you would like to set index to column, you can annotate with [Index].

For example,

public class User
{
    [Key]
    public string Id { get; set; }

    [Index]
    public string Email { get; set; }

    ...
}

You can also set composite and/or unique index.

public class Location
{
    [Index(Name = "IX_Zone_Area", Order = 1, IsUnique = true)]
    public string Zone { get; set; }

    [Index(Name = "IX_Zone_Area", Order = 2, IsUnique = true)]
    public string Area { get; set; }

    ...
}
OnDelete

You can set delete policy on column.

public class OrderItem
{
    public string SkuId { get; set; }

    [OnDelete(DeleteBehavior.Restrict)]
    public Sku Sku { get; set; }

    ...
}

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 159 8/30/2019