FluidNames is the Entity Framework Core plugin for create some enhancements in your EF Core database context.
- Create short names for entity tables, fields, indexes.
- Create
ValueConverter
expressions for references. - Adds the ability to store several data types in one field.
- Adds some attributes for use instead of using Fluent API.
public class AnimalOwner
{
[Key]
public Guid Id {get;set;}
public string Name {get;set;}
}
public class Dog
{
//... you code ...//
public AnimalOwner Owner {get;set;} // This is valid Type, because of ValueConverter is present!
}
You can disable autocreate ValueConverter
for entity/property by setting [NoValueConverter]
attribute.
If entity property can be several types, you can describe this property as object type. This plugin understands it, and create Value converter for this case. In database, this property will store as JSON string. If this value is reference, only key of referenced entity will be store.
public class AnimalOwner
{
[Key]
public Guid Id {get;set;}
public string Name {get;set;}
}
public class Dog
{
//... you code ...//
public object Owner {get;set;} // You can store any data in this property
}
You can disable autocreate ValueConverter
for entity/property by setting [NoValueConverter]
attribute.
FluidNames
will autonumerate you entities and automaticly setup table names and field.
- Install FluidNames Package
Run the following command in the Package Manager Console
to install FluidNames
PM> Install-Package FluidNames
- Adjust you entities
[FluidName("Animal")] // setup start table names
[FluidPropertyName("field")] // setup start field names
public class Animal
{
[Key]
[NoFluidName] // mark field name as constant (no fluid)
public Guid Id {get;set;}
[NoFluidName]
public string Code {get;set;}
[NoFluidName]
public string Name {get;set;}
}
public class Dog: Animal
{
public string Kind {get;set;}
public int Age {get;set;}
}
public class Cat: Animal
{
public string Nick {get;set;}
public int Age {get;set;}
}
In this case, tables will be Animal1
... Animal(n)
and fields will be Id
,Code
,Name
...,field1
,...,field(n)
- Create fluid names in OnModelCreating method
public class MyDBContext : DbContext
{
public MyDBContext(DbContextOptions<MyDBContext> options)
: base(options)
{ }
public DbSet<Dog> Dogs { get; set; }
public DbSet<Cat> Cats { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// create fluid names for you entities.
modelBuilder.CreateFluidNames( this);
}
}
[KeyPart] // mark the property as a part of key
This is equivalent Fluent API method, but it can be done at top hierarchy level
modelBuilder.Entity<>().HasKey(v => new { v.ID, v.CODE})
[NoBaseType] // equivalent Fluent API HasBaseType((Type)null) method
This is equivalent Fluent API method, but it can be done at top hierarchy level
modelBuilder.Entity<>().HasBaseType((Type)null)
[Index("IDX_Field1", true)] // designate that property is the part of index
This is equivalent Fluent API method, but it can be done at top hierarchy level
modelBuilder.Entity<>().HasIndex(...).IsUnique()
[IsRequiredAsReference] // you can set Entity, that referenced this Entity column does not allow null
This is equivalent Fluent API method, but you can mark all reference for this Entity as required
modelBuilder.Entity<>().Property(<referencing property>).IsReqiured()
[DefaultSQLValueForReference("sql expression")] // you can set default value for reference this Entity
For property, equivalent is
[DefaultSQLValue("sql expression")] // you can set default sql value for property
This is equivalent Fluent API method, but you can mark all reference for this Entity as required
modelBuilder.Entity<>().Property(<referencing property>).HasDefaultValueSql("sql expression")
You can set xmin system column as concurency token. For details, please refer to Npgsql documentation
[UseXminAsConcurrencyToken] // set xmin system column as concurency token
modelBuilder.Entity<>().UseXminAsConcurrencyToken()