2

对于工作中的项目,我创建了类似于以下内容的实体:

public class ObjectA
{
    public Guid ObjectAGUID { get; set; } // key
    public string Name { get; set; }
    public int Number { get; set; }
}

public class ObjectB
{
    public Guid ObjectBGUID { get; set; } //key
    public string Name { get; set; }
    public int Number { get; set; }
}

public class Note
{
    public Guid NoteGUID { get; set; } // key
    public Guid AssociationGUID { get; set; }
    public string Text { get; set; }
    public string NoteType { get; set; }
} 

ObjectA 和 ObjectB 都与 Note 具有一对多关系,其中 Note.AssociatedGUID 是 A 和 B 各自实体键的依赖属性。换句话说,A 和 B 可以有任意数量的 Note 实体与其中任何一个相关联。

我的问题:

当我在 Note.AssociatedGUID = ObjectA.ObjectAGUID 创建一个新的 ObjectA 和一个新的 Note 时,我在 DBContext.SaveChanges() 上收到以下错误:

Entities in 'Entities.Notes' participate in the 'ObjectBNote' relationship. 0 related 'ObjectB' were found. 1 'ObjectB' is expected.

沉思

如果我想同时为 ObjectA 和 ObjectB 创建注释,是否需要创建单独的注释实体(例如 NoteA 和 NoteB)来执行此操作?如果是这样,这似乎是多余的。

或者,我是否需要在 Note 上为每个父实体设置一个单独的可为空的外键?虽然更易于管理,但这似乎也有点矫枉过正。

public class Note
{
    public System.Guid NoteGUID { get; set; }
    public System.Guid? ObjectAGUID { get; set; }
    public System.Guid? ObjectBGUID { get; set; }
    public string Text { get; set; }
    public string NoteType { get; set; }
}

帮助?

4

0 回答 0