entity-framework-core - 将 efCore 从 2.2 更新到 3.1 时拥有的实体
问题描述
我正在将基于 DDD 设计原则的现有项目从 efcore 2.2 迁移到 efcore 3.1。数据库设置基于 Julie Lerman 几年前撰写的系列文章。
一般来说,这很好,但我很难解决与拥有实体一起发布的问题,特别是这个错误消息:
InvalidOperationException: The type 'ApplicationCore.Entities.UserAggregate.Email' cannot be configured as non-owned because an owned entity type with the same name already exists
这两个实体是:
public class User
{
public int Id { get; private set; }
public Guid GuidId { get; private set; }
public Email Email {get; private set;}
}
它是“拥有”的实体
public class Email
{
public string Address { get; private set; }
}
以前在 EfCore 2.2 中的配置是:
private static void ConfigureUser(EntityTypeBuilder<User> builder)
{
builder.HasKey(s => s.Id);
builder.Property(s => s.GuidId)
.IsRequired();
builder.OwnsOne(u => u.Email);
}
据我了解,我应该在 efcore3.1 中做的是将其更新为:
private static void ConfigureUser(EntityTypeBuilder<User> builder)
{
builder.HasKey(s => s.Id);
builder.Property(s => s.GuidId)
.IsRequired();
builder.OwnsOne(u => u.Email).WithOwner();
}
OnModelCreating()
除了这个配置方法之外,该方法中的其他实体还有几个
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<ForecastSetup>(ConfigureForecastSetup);
…
builder.Entity<User>(ConfigureUser);
// Remove internal property
foreach (var entityType in builder.Model.GetEntityTypes())
{
builder.Entity(entityType.Name).Ignore("IsDirty");
}
}
异常将从builder.Entity(entityType.Name).Ignore("IsDirty")
行中抛出。
就是这样。但是,这使差异为零,并且再次出现相同的错误。
我无法运行 add-migrations 来测试是否设置了其他东西,因为抛出了异常,我不确定如果我删除 ContextModelSnapshot 会发生什么......</p>
解决方案
感谢@IvanStoev,请参阅他在评论中链接到的问题。
配置是正确的,我的问题是在尝试删除 Shadow 属性时出现的
// Remove shadow property for entities which are not owned
foreach (var entityType in builder.Model.GetEntityTypes().Where(e => !e.IsOwned()))
{
builder.Entity(entityType.Name).Ignore("IsDirty");
}
推荐阅读
- arrays - 运行 Apps 脚本以迭代数组时出现类型错误
- node.js - heroku 没有显示 Procfile 文件 Discord.js
- authentication - 使用 openssl 的 .p7b 文件的两种方式 SSL 身份验证
- amazon-web-services - AWS Glue DynamicFrames 和下推谓词
- javascript - 刷新页面后 Socket.io 聊天多个帖子
- ios - 我的两个视图之间的通知/观察者只有在我来回后才有效
- android - Android 蓝牙 API 未找到任何设备
- c++ - 受保护的继承 - 为什么不允许某个命令
- azure-devops - 如何将 Outlook 连接到 DevOps?
- python - 在 PyTorch 中计算 4D 张量的最大值和最小值