“必须通过导航从另一实体类型引用拥有的实体类型”。 [英] "The owned entity type requires to be referenced from another entity type via a navigation"

查看:122
本文介绍了“必须通过导航从另一实体类型引用拥有的实体类型”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个名为Person的实体。从Person继承的用户有两种类型:Student和Professor。

I have an entity in my application called Person. There are two types of users, Student and Professor, that inherit from Person.

每个人都有设置属性:

public abstract class Person
{
    public Guid UserId { get; set; }
    public string Name { get; set; }

    public PersonSettings Settings { get; set; }
}

public class Student : Person
{
}

public class Professor : Person
{
}

我的PersonSettings类只是几个属性。它不是要存储在数据库中的实体,因此我将其标记为拥有:

My PersonSettings class is just a couple of properties. It isn't an entity to be stored in the database, so I marked it as Owned:

[Owned]
public class PersonSettings
{
    public bool NotificationsEnabled { get; set; }
    public int GymPassId { get; set; }
}

这些作为json存储在数据库中,我正在使用EF在我的Person实体配置中用于对其进行序列化和反序列化的核心转换值:

These are stored in the database as json, which I'm using EF Core conversion values in my Person entity configuration to serialize and deserialize it:

builder.Property(p => p.Settings).HasConversion(
    s => JsonConvert.SerializeObject(s, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
    s => JsonConvert.DeserializeObject<PersonSettings>(s, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }));

但是当我尝试运行我的应用程序并进行数据库迁移时,出现了错误提示

But when I try to run my application and do a database migration, I'm getting an error saying


拥有实体类型 PersonSettings需要通过导航从另一个实体类型中引用。将导航添加到指向 PersonSettings的实体类型。

The owned entity type 'PersonSettings' requires to be referenced from another entity type via a navigation. Add a navigation to an entity type that points at 'PersonSettings'.

我应该在这里做什么?我在错误消息上找不到任何内容。不确定与Person是抽象类是否有关系。

What am I supposed to do here? I couldn't find anything on the error message. Not sure if it has anything to do with Person being an abstract class.

推荐答案

我也无法复制,但您不会

I also can't repro, but you don't need an Owned Type here.

使用拥有的类型是对非标量属性使用JSON序列化的替代。使用拥有的类型时,类型与引用它的实体一起存储。因此,如拥有类型EF会为人表创建包含Settings_NotificationEnabled和Settings_GymPassId的单独列。

Using Owned Types is an alternative to using JSON serialization of a non-scalar property. When using Owned Types the type is stored along with the Entity that references it. So as Owned Type EF would create the Person table with separate columns for Settings_NotificationEnabled, and Settings_GymPassId.

因此,您可以简单地删除OwnedAttribute并确保您不声明使其成为DbContext中属性类型为 DbSet< PersonSettings> 的实体。

So you can simply remove the OwnedAttribute, and ensure that you don't declare it to be an Entity with a property of type DbSet<PersonSettings> in your DbContext.

选择,在这种情况下,我通常会使用Owned Type,因此您可以通过各个PersonSettings属性查询数据库。

As to which to pick, I would generally use an Owned Type for this scenario, so you could query the database by the individual PersonSettings properties.

使用非标量属性的JSON转换在拥有集合的情况下很有用,因为EF Core当前不支持拥有类型的集合。

Using JSON conversion of a non-scalar property is useful for the case where you have a collection, because EF Core does not currently support collections of Owned Types.

这篇关于“必须通过导航从另一实体类型引用拥有的实体类型”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆