This question already has answers here:
Entity Framework not including columns with default value in insert into query
(2个答案)
3年前关闭。
我正在使用EF Core 2.0。我有一个包含4列的表格,其中PK由所有4列组成。列之一(IsDefault)是数据库中的位字段。如果插入IsDefault设置为true的记录,则一切正常。如果插入IsDefault设置为false的记录,则会出现以下异常:
我创建了一个非常简化的版本,并且能够通过我的单个表,单个实体,单个记录,单个单元测试实现来重现该问题。
我的数据库表:
我的dbContext:
我的实体:
我的单元测试:
我已经在这个头上挠了一段时间了。我不知道。任何帮助将不胜感激!
谢谢!
(2个答案)
3年前关闭。
我正在使用EF Core 2.0。我有一个包含4列的表格,其中PK由所有4列组成。列之一(IsDefault)是数据库中的位字段。如果插入IsDefault设置为true的记录,则一切正常。如果插入IsDefault设置为false的记录,则会出现以下异常:
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
我创建了一个非常简化的版本,并且能够通过我的单个表,单个实体,单个记录,单个单元测试实现来重现该问题。
我的数据库表:
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
我的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))");
});
}
}
我的实体:
public partial class ChromeMileageRestrictionTest
{
public string TermId { get; set; }
public string Mileage { get; set; }
public decimal Residual { get; set; }
public bool IsDefault { get; set; }
}
我的单元测试:
[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();
}
}
我已经在这个头上挠了一段时间了。我不知道。任何帮助将不胜感激!
谢谢!
最佳答案
重复的:Entity Framework not including columns with default value in insert into query
TLDR:我从数据库和DbContext映射中都删除了IsDefault字段上False的默认值。这样就解决了问题。
关于c# - EF Core 2.0-添加时出现System.NotSupportedException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46571347/
10-11 18:11