小编Jam*_*ame的帖子

将值对象添加到 EF 实体 - 实体类型无法配置为拥有的,因为它已配置为非拥有的

我们收到以下错误,该错误似乎仅在将日期时间添加到值对象时才会发生。'实体类型'TimeWindow'无法配置为拥有的,因为它已被配置为非拥有的。如果您想覆盖以前的配置,请首先通过调用“忽略”从模型中删除实体类型。

值对象类:

public class TimeWindow : ValueObject
    {
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }

        private TimeWindow()
        {
        }

        public TimeWindow(
            DateTime? startTime,
            DateTime? endTime)
        {
            StartTime = startTime;
            EndTime = endTime;
        }

        protected override IEnumerable<object> GetAtomicValues()
        {
            yield return StartTime;
            yield return EndTime;
        }
    }
Run Code Online (Sandbox Code Playgroud)

在 OnModelCreating 内部,我们添加了 OwnsOne 关系:

builder.Entity<Manifest>(b =>
        {
            b.ToTable(DistributionConsts.DbTablePrefix + "Manifests", DistributionConsts.DbSchema);
            b.ConfigureByConvention();
            b.OwnsOne(b => b.TimeWindow);
        });
Run Code Online (Sandbox Code Playgroud)

我们将 TimeWindow 值对象添加到的实体:

public class Manifest : FullAuditedAggregateRoot<Guid>
    {
        protected …
Run Code Online (Sandbox Code Playgroud)

entity-framework entity-framework-core asp.net-core abp

6
推荐指数
1
解决办法
6003
查看次数