[英]C# EF Core unable to change AK alternative key
我遇到了 EFCore 问题,正是当我尝试更新具有备用键并且也是另一个实体上的外键的实体时。 我有以下实体:
public class IdentityProvider
{
public int Id { get; set; }
public string Scheme { get; set; }
public string DisplayName { get; set; }
public bool Enabled { get; set; }
public string Type { get; set; }
public string Properties { get; set; }
public DateTime Created { get; set; }
public DateTime? Updated { get; set; }
public DateTime? LastAccessed { get; set; }
public bool NonEditable { get; set; }
public virtual IdentityProviderConfiguration IdentityProviderConfiguration { get; set; }
}
public class IdentityProviderConfiguration
{
public string Scheme { get; set; }
public string Options { get; set; }
public virtual IdentityProvider IdentityProvider { get; set; }
}
以及这些 EF Core 配置:
public class IdentityProviderConfiguration : IEntityTypeConfiguration<IdentityProvider>
{
public void Configure(EntityTypeBuilder<IdentityProvider> entity)
{
entity.HasIndex(e => e.Scheme, "AK_IdentityProviders_Scheme")
.IsUnique();
entity.HasIndex(e => e.Scheme, "IX_IdentityProviders_Scheme")
.IsUnique();
entity.Property(e => e.DisplayName).HasMaxLength(200);
entity.Property(e => e.Scheme)
.IsRequired()
.HasMaxLength(200);
entity.Property(e => e.Type)
.IsRequired()
.HasMaxLength(20);
entity.HasOne(e => e.IdentityProviderConfiguration)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
entity.ToTable("IdentityProviders");
}
}
public class IdentityProviderConfigurationConfiguration : IEntityTypeConfiguration<IAM.BackOffice.Core.Entities.AuthZ.IdentityProviderConfiguration>
{
public void Configure(EntityTypeBuilder<Core.Entities.AuthZ.IdentityProviderConfiguration> entity)
{
entity.HasKey(e => e.Scheme);
entity.HasIndex(e => e.Scheme, "IX_IdentityProviderConfigurations_Scheme")
.IsUnique();
entity.Property(e => e.Scheme).HasMaxLength(200);
entity.HasOne(d => d.IdentityProvider)
.WithOne(p => p.IdentityProviderConfiguration)
.HasPrincipalKey<IdentityProvider>(p => p.Scheme)
.HasForeignKey<Core.Entities.AuthZ.IdentityProviderConfiguration>(d => d.Scheme);
entity.ToTable("IdentityProviderConfigurations");
}
}
我正在尝试在没有任何 IdentityProviderConfiguration 子项的情况下更新 IdentityProvider 实体上的架构属性。 不幸的是,我总是从 EFCore 收到错误消息,例如:
System.InvalidOperationException:属性“IdentityProvider.Scheme”是键的一部分,因此不能修改或标记为已修改。 要使用标识外键更改现有实体的主体,首先删除依赖项并调用“SaveChanges”,然后将依赖项与新主体关联。 at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetPropertyModified(IProperty property, Boolean changeState, Boolean isModified, Boolean isConceptualNull, Boolean acceptChanges) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectValueChange(InternalEntityEntry entry, IProperty property) at Microsoft. EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.LocalDetectChanges(InternalEntityEntry entry) 在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectChanges(IStateManager stateManager) 在 Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker.DetectChanges() 在 Microsoft.EntityFrameworkCore.DbContext.TryDetectChanges( )在 Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(布尔型 acceptAllChangesOnSuccess,CancellationToken 取消令牌)
我怎样才能解决这个问题? 我希望我很清楚。
万分感谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.