首页 > 解决方案 > 缺少 RelationalTypeMapping 实体框架核心

问题描述

我已经使用流利的模型构建器手动设置了我的实体框架映射,并且在我查询时一切正常,但是在尝试创建实体时,这就是我得到的:

值不能为空。参数名称:relationalTypeMapping

这是堆栈跟踪:

在 Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T 值,字符串参数名称)
在 Microsoft.EntityFrameworkCore.Storage.Internal.TypeMappedRelationalParameter..ctor(字符串 invariantName,字符串名称,RelationalTypeMapping 关系类型映射,Nullable 2 参数,CancellationToken 取消令牌)在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IReadOnlyList 3.CreateAsync(TDto dto ) 上的 Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func 4 verifySucceeded, CancellationToken cancelToken) )1 nullable)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBuilder.AddParameter(String invariantName, String name, IProperty property)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommandBuilderExtensions.AddParameter(IRelationalCommandBuilder commandBuilder, String invariantName, String name, IProperty property) at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.CreateStoreCommand() at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(DbContext _, ValueTuple
4 operation, Func1 entriesToSave, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) at FastTrack.EntityFramework.Services.EntityFrameworkService

这是我的映射代码:

   builder.Entity(typeof(Page), b =>
            {
                b.HasOne(typeof(Site))
                    .WithMany()
                    .HasForeignKey("SiteId")
                    .IsRequired();
            });

            builder.Entity<Site>()
                .HasMany(x => x.Pages)
                .WithOne(x => x.Site)
                .HasForeignKey("SiteId")
                .IsRequired();

以下是实体:

  public class Site
    {
        public int Id {get; set;}
        public string Name {get; set;}
        public IList<Page> Pages { get; set; }
        public string Domain { get; set; }
    }


   public class Page
    {
        public int Id {get; set;}
        public string Name {get; set;}
        public IList<PageSelector> PageSelectors { get; set; }
        public string Path { get; set; }
        public Site Site { get; set; }
        [ForeignKey("Site")]
        public int SiteId { get; set; }
    }

什么可能导致这种情况发生?映射似乎设置正确,一切正常,直到我想在数据库中插入一些东西。

标签: c#entity-framework-core

解决方案


推荐阅读