Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Commented Unassigned: Issues mixing conventions and entity inheritance [1545]

$
0
0
I'm using current EF 6.0 code - my own local build of EF against the master branch, synced an hour or two ago. I synched just to make sure that the problems I'm seeing weren't already fixed.

I've created an xunit test to show the problems. The fundamental problem is that I'm not able to use these 3 things together:
* A custom DateTime convention (same behavior for both lightweight convention and a convention class)
* A TPT entity type with a base class
* Configuring the DateTime precision (overriding the convention) for a property in the entity.

The code is attached.

The crux of the problem is demonstrated here:
``` C#
internal class TablePerTypeDbContext : DbContext
{

public DbSet<DerivedEntity> Entities { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Lightweight convention for DateTime properties: Use SQL datetime2 type, default precision 1.
modelBuilder.Properties<DateTime>().Configure(dateTimeConfig =>
{
dateTimeConfig.HasColumnType("datetime2");
dateTimeConfig.HasPrecision(1);
});

// Use table per type
modelBuilder.Entity<DerivedEntity>().Map(m => m.MapInheritedProperties());

// Set precision to 3 for LastModified property (overrides the convention)
// BUG: No combination of uncommenting the following 2 lines works as it should
//modelBuilder.Entity<DerivedEntity>().Property(e => e.LastModified).HasPrecision(3);
//modelBuilder.Entity<BaseEntity>().Property(e => e.LastModified).HasPrecision(3);

}
}
```

EF Behavior varies wildly depending on which of the last 2 lines are uncommented:
1. With both commented out, table dbo.DerivedEntities is created, and LastModified column has precision 1 (expected behavior).
2. With the DerivedEntity line uncommented, InvalidOperationException is thrown: Conflicting configuration settings were specified for property 'LastModified' on type 'Base.DataAccess.UnitTests.BaseEntity': Precision = 3 conflicts with Precision = 1
3. With both lines uncommented, table dbo.BaseEntities is created, and LastModified column has precision 3 (precision is right, but table name is wrong - acts like TPH instead of TPT)
4. With the DerivedEntity line commented out, and BaseEntity line uncommented, the behavior is the same as #3: table dbo.BaseEntities is created, and LastModified column has precision 3 (precision is right, but table name is wrong - acts like TPH instead of TPT)

Comments: For case 2 (where the line ``` C# modelBuilder.Entity<DerivedEntity>().Property(e => e.LastModified).HasPrecision(3); ``` is uncommented, and the InvalidOperationException is thrown, I tried working around the problem using a non-lightweight convention. The attached source file (DateTime2ColumnConvention1.cs) uses a lightweight convention for setting the ColumnType to datetime2, and a `IConceptualModelConvention<EdmProperty>` to try to set the default Precision later in the model-building process. While it runs (no exception is thrown), and the `EdmProperty.Precision` value is successfully set, it doesn't successfully change the default precision value in the output SQL. The precision is set to 7 in the DDL SQL.

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>