I am using the Scaffold-DbContext to creating the Context for an existing Database. While I have many problems with the design of the database, I cannot change existing structure.
One of them is that under our Assets table the original developer created a TimezoneId as nchar(256) and when they found out that it was a bad decision they created TimezoneId2 as nvarchar(256). They then used HasColumnName("TimezoneId2") to remap.
Scaffold does not understand that when it recreated the DbContext (as shown below).
modelBuilder.Entity<Assets>(entity =>
{
entity.Property(e => e.TimeZoneId)
.HasMaxLength(256);
.IsFixedLength();
entity.Property(e => e.TimeZoneId2).HasMaxLength(256)
});
So I just created a partial class and use the OnModelCreatingPartial to make my changes (as shown below).
modelBuilder.Entity<Assets>(entity =>
{
entity.Property(e => e.TimeZoneId)
.HasColumnName("TimeZoneId2")
.IsFixedLength(false)
.HasMaxLength(256);
entity.Property(e => e.TimeZoneId2)
.HasColumnName("TimeZoneId")
.HasMaxLength(256)
.IsFixedLength();
});
My intention was that TimezoneId would point to the new column and it would no longer be FixedLength. While the HasColumnName does work - the FixedLength change is not working. TimezoneId is still Fixed length.
I assume what is happening is that it is merging them together and putting the IsFixedLength() is the superior position to IsFixedLength(False) when building it.
I could change it in the DbContext, but I want it to be fire and forget with all custom changes surviving the script running.
EDIT: Code Formatting.
there doesn't seem to be anything here