c# - EF Core 3.1:我不理解脚手架代码中的配置错误
问题描述
我有以下我不明白的错误和运行时:
从 'GpsBoxSubTypeDes.GpsBox' 到 'GpsBoxSubType.GpsBoxSubTypeDes' 与外键属性 {'GpsBoxTypeId' : int, 'GpsBoxSubTypeId' : int} 的关系不能以主键 {'GpsBoxSubTypeId' : int} 为目标,因为它不兼容。为此关系配置一个主键或一组兼容的外键属性。
这两个实体的脚手架代码和配置如下:
public partial class GpsBoxSubType
{
public GpsBoxSubType()
{
Vehicule = new HashSet<Vehicule>();
AdditionalInit();
}
public int GpsBoxTypeId { get; set; }
public int GpsBoxSubTypeId { get; set; }
public string Name { get; set; }
public int IconId { get; set; }
public virtual GpsBoxType GpsBoxType { get; set; }
public virtual Icon Icon { get; set; }
public virtual GpsBoxSubTypeDes GpsBoxSubTypeDes { get; set; }
public virtual ICollection<Vehicule> Vehicule { get; set; }
}
builder.Entity<GpsBoxSubType>(entity =>
{
entity.HasKey(e => new { e.GpsBoxTypeId, e.GpsBoxSubTypeId });
entity.Property(e => e.GpsBoxTypeId).HasColumnName("GpsBoxTypeID");
entity.Property(e => e.GpsBoxSubTypeId).HasColumnName("GpsBoxSubTypeID");
entity.Property(e => e.IconId).HasDefaultValueSql("((33554432))");
entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(50);
entity.HasOne(d => d.GpsBoxType)
.WithMany(p => p.GpsBoxSubType)
.HasForeignKey(d => d.GpsBoxTypeId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_GpsBoxSubType_GpsBoxTypeID");
entity.HasOne(d => d.Icon)
.WithMany(p => p.GpsBoxSubType)
.HasForeignKey(d => d.IconId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_GpsBoxSubType_IconId");
});
public partial class GpsBoxSubTypeDes
{
public int GpsBoxTypeId { get; set; }
public int GpsBoxSubTypeId { get; set; }
public int Lcid { get; set; }
public string Description { get; set; }
public virtual GpsBoxSubType GpsBox { get; set; }
}
builder.Entity<GpsBoxSubTypeDes>(entity =>
{
entity.HasKey(e => new { e.GpsBoxTypeId, e.GpsBoxSubTypeId });
entity.Property(e => e.GpsBoxTypeId).HasColumnName("GpsBoxTypeID");
entity.Property(e => e.GpsBoxSubTypeId).HasColumnName("GpsBoxSubTypeID");
entity.Property(e => e.Description)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.Lcid).HasColumnName("LCId");
entity.HasOne(d => d.GpsBox)
.WithOne(p => p.GpsBoxSubTypeDes)
.HasForeignKey<GpsBoxSubTypeDes>(d => new { d.GpsBoxTypeId, d.GpsBoxSubTypeId })
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_GpsBoxSubTypeDes_GpsBoxSubType");
});
编辑:添加缺少的实体和配置:
public partial class GpsBoxType
{
public GpsBoxType()
{
GpsBoxConfiguration = new HashSet<GpsBoxConfiguration>();
GpsBoxSubType = new HashSet<GpsBoxSubType>();
GpsBoxTypeDetail = new HashSet<GpsBoxTypeDetail>();
Vehicule = new HashSet<Vehicule>();
AdditionalInit();
}
[JsonProperty(Order = 1)]
public int GpsBoxTypeId { get; set; }
[JsonProperty(Order = 2)]
public string Name { get; set; }
[JsonProperty(Order = 3)]
public int Category { get; set; }
[JsonProperty(Order = 4)]
public int IconId { get; set; }
[JsonProperty(Order = 5)]
public GpsBoxTypeDes GpsBoxTypeDes { get; set; }
[JsonProperty(Order = 6)]
public virtual ICollection<GpsBoxConfiguration> GpsBoxConfiguration { get; set; }
[JsonProperty(Order = 8)]
public virtual ICollection<GpsBoxSubType>
GpsBoxSubType { get; set; }
[JsonProperty(Order = 9)]
public virtual ICollection<GpsBoxTypeDetail>
GpsBoxTypeDetail { get; set; }
[JsonProperty(Order = 10)]
public Icon Icon { get; set; }
[JsonProperty(Order = 11)]
public virtual ICollection<Vehicule> Vehicule { get; set; }
}
builder.Entity<GpsBoxType>(entity =>
{
entity.Property(e => e.GpsBoxTypeId)
.HasColumnName("GpsBoxTypeID")
.ValueGeneratedNever();
entity.Property(e => e.Category).HasDefaultValueSql("((1))");
entity.Property(e => e.IconId).HasDefaultValueSql("((33554432))");
entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(50);
entity.HasOne(d => d.Icon)
.WithMany(p => p.GpsBoxType)
.HasForeignKey(d => d.IconId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_GpsBoxType_Icon");
编辑 2:这是显示我所期待的堆栈跟踪,EF 使用的是约定而不是配置。
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNoShadowKeys(IModel model, IDiagnosticsLogger`1 logger)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
at Microsoft.EntityFrameworkCore.SqlServer.Internal.SqlServerModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.ValidatingConvention.ProcessModelFinalized(IConventionModelBuilder modelBuilder, IConventionContext`1 context)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelFinalized(IConventionModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelFinalized(IConventionModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.FinalizeModel()
at Microsoft.EntityFrameworkCore.ModelBuilder.FinalizeModel()
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_3(IServiceProvider p)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.get_Model()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.CheckState()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
at Microsoft.EntityFrameworkCore.RelationalQueryableExtensions.FromSqlRaw[TEntity](DbSet`1 source, String sql, Object[] parameters)
at RTE.Technologies.SafeProtect.Common.Data.DataBridge.Core.RegistrationDataBridge.GetDevicesToRetrogradeToAuthorizedStatus() in D:\AzureDevops\SafeProtect.Common\SafeProtect.Common.Data\DataBridge\Core\RegistrationDataBridge.cs:line 171
at RTE.Technologies.SafeProtect.Common.DummyConsole.Program.Main(String[] args) in D:\AzureDevops\SafeProtect.Common\SafeProtect.Common.Data.ConsoleTest\Program.cs:line 57
所以问题很可能在我这边。
编辑 3:错误确实在我这边,我没有调用 GpsBoxSubType 实体 8=| 的配置
解决方案
推荐阅读
- python - Python 模块的大小是否有限制?
- swift - AVCaptureMetadataOutput().rectOfInterest 不工作
- angular - 如何在不以角度重置表单的情况下重置验证错误?
- python - 基于现有 id 创建唯一、可传递的 id 的最简单方法
- python - 如何在python的排列中找到最大的数字?
- json - 如何使用 http 模块在 Angular 7 中读取文件 json?
- ckeditor - contentsCss 类显示两次
- json - 使用 NewtonSoft Missing Items 从 JSON 解析嵌套数组
- postgresql - 需要确定将数据复制到 PostgreSQL 的驱动路径
- javascript - 获取 iTunes 搜索 API GET