There is still a lot of flaws in EntityFrameworkCore. Many of them is really irritating. That's why first name of this library was EfCore.Irritation :).
EfCore.Shaman offers simple solution for some of problems. For more information please visit http://efcoreshaman.com/.
-
Install nuget package
Install-package EfCore.Shaman
-
Enter following code in
Up
method in eachMigration
class:migrationBuilder.FixMigrationUp<YourDbConextClass>();
-
Enter following code in
OnModelCreating
method inDbContext
class:this.FixOnModelCreating(modelBuilder);
Ef uses its own sorting method for columns in table. Primary key fields are first followed by other fields in alphabetical order. Neither natural properties order in 'code first' classes nor ColumnAttibute.Order
value is used for sorting.
To change this behaviour put code below at the end of protected override void Up(MigrationBuilder migrationBuilder)
method in each class derived from Migration
.
migrationBuilder.FixMigrationUp<YourDbConextClass>();
New column order is based on natural properties order in 'code first' classes. This can be overrided by ColumnAttibute.Order
value.
EfCore doesn't support annotation for index creation. Each index definition must be in OnModelCreating
method in DbContext
class. It is inconsequence leading to worse code readablity.
EfCore.Shaman offers own IndexAttribute
and UniqueIndexAttribute
. In order to use this attributes put following code at the end of 'OnModelCreating' method in you DbContext
class.
this.FixOnModelCreating(modelBuilder);
Just use IndexAttribute
and UniqueIndexAttribute
without any parameter like below:
[Index]
public Guid InstanceUid { get; set; }
[Index("MyName")]
public Guid InstanceUid { get; set; }
To create index with more than one column put IndexAttribute
or UniqueIndexAttribute
with some name and number related to field position in in index
[Index("Idx_myName", 1)]
public Guid InstanceUid { get; set; }
[Index("Idx_myName", 2)]
public Guid BoxUid { get; set; }
You can also use names starting with @
sign. That names will be replaced by name generated automatically by EF.
[Index("@1", 1)]
public Guid InstanceUid { get; set; }
[Index("@1", 2)]
public Guid BoxUid { get; set; }
IndexAttribute
and UniqueIndexAttribute
contains bool IsDescending
property designed for changing sorting order of index column. This is not yet supported.
[Index("@1", 1)]
public Guid InstanceUid { get; set; }
[Index("@1", 2, true)]
public Guid BoxUid { get; set; }
DecimalTypeAttribute
can be used for decorating decimal property. It affects changing decimal column type definition. For example
[DecimalType(18, 6)]
public decimal Amount { get; set; }
DefaultValueAttribute
and DefaultValueSqlAttribute
can be used for setting default values on columns. For example
[DefaultValue(true)]
public bool IsActive { get; set; }
[DefaultValueSql("getdate()")]
public DateTime DateCreated { get; set; }
In order to support SqlServer features add nuget package EfCore.Shaman.SqlServer
and turn on necessary options in coniguration, i.e.
migrationBuilder.FixMigrationUp<YourDbConextClass>(ShamanOptions.Default.WithSqlServer());
and
this.FixOnModelCreating(modelBuilder, ShamanOptions.Default.WithSqlServer());
Collations specific to SqlServer is recognized by reflection scaner, i.e.
[SqlServerCollation(KnownCollations.Polish100CiAi)]
public string Code { get; set; }
Moreover SqlServerCollationAttribute
can be used for class annotation in order to set default collation for all text columns (not supported yet).
Current release allows to specify column collation while only table is created.
Assembly distributed with nuget package is signed with key.snk
that is not included with github repository. mksnk.bat
script file is included instead. It it running automatically during building process.
Due to MsBuild issue csproj and packages.config can't coexist with xproj and project.json.
My own priority (sorry) is to maintain .Net framework version so files necessary for .NET Core compilation are provided in separate directory.
- Full column collation support
- .NetCore and .NetFramework release in the same time
- Not supported descending field order in indexes