I am using EF Core 2.0. I have a table with 4 columns where the PK is made up of all 4 columns. One of the columns (IsDefault) is a bit field in the database. If I insert a record with IsDefault set to true everything works fine. If I insert a record with IsDefault set to false I get the following exception:
System.NotSupportedException occurred
HResult=0x80131515
Message=The 'IsDefault' on entity type 'ChromeMileageRestrictionTest' does not have a value set and no value generator is available for properties of type 'bool'. Either set a value for the property before adding the entity or configure a value generator for properties of type 'bool'.
Source=<Cannot evaluate the exception source>
StackTrace:
at Microsoft.EntityFrameworkCore.ValueGeneration.ValueGeneratorSelector.Create(IProperty property, IEntityType entityType)
at Microsoft.EntityFrameworkCore.ValueGeneration.RelationalValueGeneratorSelector.Create(IProperty property, IEntityType entityType)
at Microsoft.EntityFrameworkCore.ValueGeneration.Internal.SqlServerValueGeneratorSelector.Create(IProperty property, IEntityType entityType)
at Microsoft.EntityFrameworkCore.ValueGeneration.ValueGeneratorSelector.<>c__DisplayClass6_0.<Select>b__0(IProperty p, IEntityType e)
at Microsoft.EntityFrameworkCore.ValueGeneration.ValueGeneratorCache.<>c.<GetOrAdd>b__3_0(CacheKey ck)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.EntityFrameworkCore.ValueGeneration.ValueGeneratorCache.GetOrAdd(IProperty property, IEntityType entityType, Func`3 factory)
at Microsoft.EntityFrameworkCore.ValueGeneration.ValueGeneratorSelector.Select(IProperty property, IEntityType entityType)
at Microsoft.EntityFrameworkCore.ValueGeneration.Internal.SqlServerValueGeneratorSelector.Select(IProperty property, IEntityType entityType)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ValueGenerationManager.Generate(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode node)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph(EntityEntryGraphNode node, Func`2 handleNode)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState entityState, Boolean forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState)
at Microsoft.EntityFrameworkCore.DbContext.Add[TEntity](TEntity entity)
at DAL.Tests.UnitTest1.TestMethod1() in C:\Users\jkruer\Source\Repos\JLM.App.ChromeIncentivesService\DAL.Tests\UnitTest1.cs:line 12
I created a VERY simplified version and am able to reproduce the problem with my single table, single entity, single record, single unit test implementation.
My Database Table:
CREATE TABLE [dbo].[ChromeMileageRestrictionTEST](
[TermID] [varchar](50) NOT NULL,
[Mileage] [varchar](50) NOT NULL,
[Residual] [decimal](18, 8) NOT NULL,
[isDefault] [bit] NOT NULL,
CONSTRAINT [PK_ChromeMileageRestrictionTEST] PRIMARY KEY CLUSTERED
(
[TermID] ASC,
[Mileage] ASC,
[Residual] ASC,
[isDefault] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ChromeMileageRestrictionTEST] ADD CONSTRAINT [DF_ChromeMileageRestrictionTEST_Residual] DEFAULT ((0)) FOR [Residual]
GO
ALTER TABLE [dbo].[ChromeMileageRestrictionTEST] ADD CONSTRAINT [DF_ChromeMileageRestrictionTEST_isDefault] DEFAULT ((0)) FOR [isDefault]
GO
My dbContext:
public partial class ReportingContext : DbContext
{
public ReportingContext()
{
}
public virtual DbSet<ChromeMileageRestrictionTest> ChromeMileageRestriction { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"MyConnectionStringGoesHere");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ChromeMileageRestrictionTest>(entity =>
{
entity.HasKey(e => new { e.TermId, e.Mileage, e.Residual, e.IsDefault })
.HasName("PK_ChromeMileageRestrictionTest");
entity.Property(e => e.TermId)
.HasColumnName("TermID")
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.Mileage)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.Residual)
.HasColumnType("decimal(18, 8)")
.HasDefaultValueSql("((0))");
entity.Property(e => e.IsDefault)
.HasColumnName("isDefault")
.HasDefaultValueSql("((0))");
});
}
}
My Entity:
public partial class ChromeMileageRestrictionTest
{
public string TermId { get; set; }
public string Mileage { get; set; }
public decimal Residual { get; set; }
public bool IsDefault { get; set; }
}
My Unit Test:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var dbContext = new ReportingContext();
dbContext.Add(new ChromeMileageRestrictionTest
{
TermId = "6078915-0-0",
Mileage = "15,000",
Residual = 45.00000000M,
IsDefault = true //THIS WORKS
});
dbContext.Add(new ChromeMileageRestrictionTest
{
TermId = "6078915-0-0",
Mileage = "15,000",
Residual = 45.00000000M,
IsDefault = false //THIS THROWS AN EXCEPTION
});
dbContext.SaveChanges();
}
}
I've been scratching my head on this one for a while. I can't figure it out. Any help would be GREATLY APPRECIATED!
Thanks!
Duplicate of this: Entity Framework not including columns with default value in insert into query
TLDR: I removed the default value of False on the IsDefault field both from the database and from the DbContext mapping. This solved the problem.
User contributions licensed under CC BY-SA 3.0