1
votes

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.

1
That's rather weird. Looks all fine to me. (I'm not familiar enough with VB, but [Property] is the same like Me.Property in this context, right?) Do you have this problem only with HasPrecision or also any other Fluent API code?Slauma
@Slauma - Yes, I believe '[Property]' is the same as 'Me.Property', but I will double check when I am back in front of my computer (I'm on a tablet now). I also notice the problem randomly with other configurations. For instance, I couldn't set the 'Key' configuration through the 'EntiyTypeConfiguration' either. I had to use the 'OnModelCreating' as well. But other configuration settings work just fine. I removed other configurations I am using from the posted code to make it easier to read.macoms01

1 Answers

2
votes

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!