小编kzm*_*jak的帖子

一对多返回空数组

我正在尝试以一对多关系连接两个对象,Workplace 可以容纳多个 People,而一个 Person 只能拥有一个 Workplace。

当我执行此代码片段并检查结果时,p1 已正确将 w1 指定为 p1.Workplace,但 w1.employees 的人员列表始终为空。

即使我将 w1 分配给 p1.Workplace 之后,是否还必须手动将 p1 添加到 w1.Employees 中?

SeedData.cs(代码片段)

var w1 = new Workplace
{
   EntryCode = "1111"
   //[...] other data I cut out for brievity
};
context.Workplaces.Add(w1);

Person p1 = new Person
{
    Name = "Spencer",
    Workplace = w1
   //[...] other data I cut out for brievity
};
context.Person.Add(p1)
context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

工作场所.cs

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace Counsel.Models
{
    [Table("Workplace")]
    public class Workplace
    {
        [Column("WorkplaceId")] …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core asp.net-core

4
推荐指数
1
解决办法
1874
查看次数

标签 统计

asp.net-core ×1

c# ×1

entity-framework-core ×1