我正在使用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 …Run Code Online (Sandbox Code Playgroud) c# sql-server entity-framework entity-framework-core .net-core
我在使用实体框架核心插入具有默认值列的记录时遇到了一个奇怪的问题。有没有其他方法来获得预期的行为,我是否错过了一些愚蠢的事情?
tldr:设置 int 属性 = 0 或 bool 为 true 会导致新字符串使用默认值 string.empty 给出 ORA-01400: 无法插入 NULL (这似乎是 Oracle 的事情)
场景:使用迁移添加新列并添加新记录会导致正常行为(插入默认值)
仅添加新记录提供的旧字段=确定
"ID" : 6, "OLDFIELD" : "ef test 1", "NEWINT" : 5, "NEWSTRING" : "def", "NEWBOOLFALSE" : 0, "NEWBOOLTRUE" : 1
添加与 .Net 默认值不同的值也有效
"ID" : 12, "OLDFIELD" : "ef test all set", "NEWINT" : 42, "NEWSTRING" : "string here", "NEWBOOLFALSE" : 1, "NEWBOOLTRUE" : 1
现在,当您为 int 指定 0 并为 bool 指定 false 时会出现问题
var test = new TestDef() …Run Code Online (Sandbox Code Playgroud)