加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – EF Core 2.0 – System.NotSupportedException On添加[复

发布时间:2020-12-15 22:47:40 所属栏目:百科 来源:网络整理
导读:参见英文答案 Entity Framework not including columns with default value in insert into query????????????????????????????????????2个 我正在使用EF Core 2.0.我有一个包含4列的表格,其中PK由所有4列组成.其中一列(IsDefault)是数据库中的位字段.如果我
参见英文答案 > Entity Framework not including columns with default value in insert into query????????????????????????????????????2个
我正在使用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:UsersjkruerSourceReposJLM.App.ChromeIncentivesServiceDAL.TestsUnitTest1.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",IsDefault = false //THIS THROWS AN EXCEPTION
            });
            dbContext.SaveChanges();
        }
    }

我一直在这个问题上摸不着头脑.我无法弄清楚.任何帮助将不胜感激!

谢谢!

解决方法

重复: Entity Framework not including columns with default value in insert into query

TLDR:我从数据库和DbContext映射中删除了IsDefault字段的默认值False.这解决了这个问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读