首页 > 解决方案 > 将列映射到 EF Core 3 中的子对象

问题描述

给出下面的示例,有没有办法在不使用表拆分或拥有类型(例如 EF6 复杂类型)的情况下将 Address 与 User 放在同一个表中?生成的 SQL 阻止我使用它,并且 EF Core 3 似乎不支持复杂类型:

    public class User
    {
        public int Id { get; set; }

        public Address Address { get; set; }

        public string UserName { get; set; }
    }


    public class Address
    {
        public string StreetAddress { get; set; }

        public string City { get; set; }

        public string State { get; set; }

        public string ZipCode { get; set; }
    }

我看到的唯一其他选择是将地址映射到它自己的表。

标签: c#sql-serverentity-framework-coreef-core-3.1

解决方案


我将使用 1 到 0..1 的关系或直接在 User 中包含属性。

尽管如此,使用 Owned Types 作为 EF 6 中的 ComplexTypes 的替代品是可怕的,如果从 SQL 的角度来看不是完全没用的话,而且我看不出有任何加入的原因。也许有人可以澄清完整性的适当理由


推荐阅读