Question

I have a solution that I'm using to test Fluent API. When trying to set the precision on a decimal, the database is only updated correctly if I put the call in the OnModelCreating method and go through the entity. When trying to use an EntityTypeConfiguration class, the database refuses to update. MilesFromNearestAirport is the property in question.

Lodging Model

Public Class Lodging
    Public Property LodgingId As Integer
    Public Property MilesFromNearestAirport As Decimal
End Class

DBContext Class

    Public Class BreakAwayContext
        Inherits DbContext

        Public Property Lodgings As DbSet(Of Lodging)
        Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
            modelBuilder.Configurations.Add(New LodgingConfiguration)
        End Sub
    End Class

Lodging Configuration Class

Public Class LodgingConfiguration
    Inherits EntityTypeConfiguration(Of Lodging)

    Public Sub LodgingConfiguration()
        [Property](Function(l) l.MilesFromNearestAirport).HasPrecision(8, 1)
    End Sub

End Class

I'm using a console app for testing. Here is the Sub Main ()

Sub Main()
    Database.SetInitializer(New DropCreateDatabaseAlways(Of BreakAwayContext))
End Sub

As you can see, I am dropping and recreating the database each run, regardless of changes to the models. If I put the decimal precision configuration setting in the OnModelCreating method, all is well.

    Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
        modelBuilder.Entity(Of Lodging).Property(Function(l) l.MilesFromNearestAirport).HasPrecision(8, 1)
    End Sub

Are there any restrictions to using the EntityTypeConfiguration or does anyone have any suggestions on where I may have gone wrong? I was going to post pictures of the resulting database table, but I'm not able to post images due to my reputation points. Please let me know if you would like anymore information about the classes, solutions, environment etc. I'm using EF6, .NET4, VB.NET, and SQL Server Express.

Was it helpful?

Solution

Okay, I found the issue in case anyone else runs into this. I am working off C# examples and converting them to VB using an online converter. I am not very familiar with C# and converted the code incorrectly.

This line Public Sub LodgingConfiguration()

Public Class LodgingConfiguration
    Inherits EntityTypeConfiguration(Of Lodging)

    Public Sub LodgingConfiguration()
        [Property](Function(l) l.MilesFromNearestAirport).HasPrecision(8, 1)
    End Sub

End Class

Should have been Public Sub New()

Public Class LodgingConfiguration
    Inherits EntityTypeConfiguration(Of Lodging)

    Public Sub New()
        [Property](Function(l) l.MilesFromNearestAirport).HasPrecision(8, 1)
    End Sub

End Class

This seems to have fixed all the issues. Thanks @Slauma for taking a look!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top