Ответ 1
Итак, у вас есть определенные стандартные модели сущностей, вот продукт с идентификатором и десятичным значком, а также все, что вам нужно и т.д.
public class Product
{
public int Id { get; set; }
public decimal Fineness { get; set; }
}
Итак, я определил инициализатор, и в этом случае база данных будет удаляться и воссоздавать любую предоставленную информацию о посеве, каждый раз, когда я запускаю и выполняю мое приложение, это будет вызываться.
public class Initializer : DropCreateDatabaseAlways<Context>
{
protected override void Seed(Context context)
{
// note how i am specifying it here as 4 digits after the decimal point
// and for the second one, 3 digits
// this is where EF precision must be configured so you can expect
// the values you tell EF to save to the db
context.Products.Add(new Product() {Id = 1, Fineness = 145.2442m});
context.Products.Add(new Product() {Id = 2, Fineness = 12.341m});
}
}
public class Context : DbContext
{
public IDbSet<Product> Products { get; set; }
public Context()
{
// I always explicitliy define how my EF should run, but this is not needed for the answer i am providing you
Configuration.AutoDetectChangesEnabled = true;
Configuration.ProxyCreationEnabled = true;
Configuration.LazyLoadingEnabled = true;
Configuration.ValidateOnSaveEnabled = true;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// so here, I am override the model configuration which is what
// EF can use in order to set-up the behaviour of how everything
// is configured in the database, from associations between
// multiple entities and property validation, Null-able, Precision, required fields etc
modelBuilder.Configurations.Add(new ProductConfiguration());
}
}
public class ProductConfiguration : EntityTypeConfiguration<Product>
{
public ProductConfiguration()
{
ToTable("Product");
HasKey(x => x.Id).Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// HAS PRECISION.
// Enforces how the value is to be stored in the database
// Here you can see I set a scale of 3, that 3 digits after
// the decimal. Notice how in my seed method, i gave a product 4 digits!
// That means it will NOT save the product with the other trailing digits.
Property(x => x.Fineness).HasPrecision(precision: 10, scale: 3);
}
}
С помощью Обозревателя объектов SQL Server я могу просмотреть свой файл localdb Example, который я сделал, чтобы увидеть, как EF настроил мою базу данных.
[TestFixture]
public class Tests
{
[Test]
public void Test()
{
Database.SetInitializer(new Initializer());
using (var ctx = new Context())
{
// assert our findings that it is indeed not what we actually specified in the seed method, because of our Entity configuration with HasPrecision.
Product product1 = ctx.Products.Find(1);
Assert.AreEqual(145.244m, product1.Fineness);
Product product2 = ctx.Products.Find(2);
Assert.AreEqual(12.341m, product2.Fineness);
}
}
}
Таким образом, нам нужно обеспечить, чтобы база данных знала, как она должна хранить наше десятичное значение, путем настройки нашего объекта с помощью конфигурации построителя модели Entity Framework, используя FluentApi
, мы можем настроить свойства с помощью EntityTypeConfiguration<T>
.