无法添加实体类型“ActivityEntity”的种子实体,因为没有为所需属性“SubjectId”提供值

问题描述 投票:0回答:1

我正在为学校创建一个 .NET 应用程序,但我遇到了这个奇怪的内部错误。

The seed entity for entity type 'ActivityEntity' cannot be added because no value was provided for the required property 'SubjectId'

这是

ActivityEntity
课程:

public record ActivityEntity : IEntity
{
    public required DateTime StartDateTime { get; set; }
    public required DateTime EndDateTime { get; set; }
    public required Room Room { get; set; }
    public required ActivityType Type { get; set; }
    public string? Description { get; set; }

    public required SubjectEntity Subject { get; init; }
    public ICollection<EvaluationEntity> Evaluations { get; init; } = new List<EvaluationEntity>();
    
    public required Guid Id { get; set; }
}

这是

SubjectEntity

public record SubjectEntity : IEntity
{
    public required string Name { get; set; }
    public required string Abbreviation { get; set; }

    public ICollection<ActivityEntity> Activities { get; init; } = new List<ActivityEntity>();
    public ICollection<StudentEntity> Students { get; init; } = new List<StudentEntity>();
    
    public required Guid Id { get; set; }
}

这是失败的测试:

public async Task AddNew_Activity_Persisted()
    {
        //Arrange
        var entity = ActivitySeeds.ActivityEntity1;

        //Act
        SISDbContextSUT.Activities.Add(entity);
        await SISDbContextSUT.SaveChangesAsync();

        //Assert
        await using var dbx = await DbContextFactory.CreateDbContextAsync();
        var actualEntities = await dbx.Activities.SingleAsync(i => i.Id == entity.Id);
        Assert.Equal(entity, actualEntities);
    }

这是代码失败的部分:

public async Task InitializeAsync()
    {
        await SISDbContextSUT.Database.EnsureDeletedAsync();
        await SISDbContextSUT.Database.EnsureCreatedAsync();
    }

我创建了一个新的

DbContext
,.NET 似乎无法链接数据库?

这是数据库架构:

enter image description here

仅活动和主题相关。

这就是定义关系的方式:

modelBuilder.Entity<StudentEntity>()
            .HasMany(i => i.Subjects)
            .WithMany(i => i.Students);

        modelBuilder.Entity<ActivityEntity>()
            .HasOne(i => i.Subject)
            .WithMany(i => i.Activities);

        modelBuilder.Entity<EvaluationEntity>()
            .HasOne(i => i.Activity)
            .WithMany(i => i.Evaluations);

        modelBuilder.Entity<EvaluationEntity>()
            .HasOne(i => i.Student)
            .WithMany(i => i.Evaluations);
c# .net
1个回答
0
投票

您与

1 - n
ActivityEntity
之间存在
SubjectEntity
关系。因此
SubjectId
中将会有一个隐式的
ActivityEntity
作为外键。所以我建议将这个隐式外键显式添加为属性。

public record ActivityEntity : IEntity
{
    public required DateTime StartDateTime { get; set; }
    public required DateTime EndDateTime { get; set; }
    public required Room Room { get; set; }
    public required ActivityType Type { get; set; }
    public string? Description { get; set; }

    public required SubjectEntity Subject { get; init; }
    
    // Add FK explicit
    public Guid SubjectId { get; init; }
    
    public ICollection<EvaluationEntity> Evaluations { get; init; } = new List<EvaluationEntity>();
    public required Guid Id { get; set; }
}

现在您可以在

SubjectId
中明确设置
ActivityEntity
进行播种。

© www.soinside.com 2019 - 2024. All rights reserved.