DB/Domain 模型中基本类型的 EF 对象包装器

EF Object wrapper around a primitive type in DB/Domain model

假设我的数据库中有一个联系人 Table,它有一个 PhoneNumber 列,它是一个字符串,例如“+44123456789”。

在我的领域模型中,我通常会有:

public class Contact {
    public int Id { get; set; }
    public string PhoneNumber { get; set; }
}

到目前为止很普通 :) 但是现在,我想将一堆非数据库逻辑与我的电话号码相关联。

我可以创建一个 PhoneNumber 对象来保存所有这些逻辑,并将我的 PhoneNumber 属性 改为 Number 对象:

public class Contact {
    public int Id { get; set; }
    public Number PhoneNumber { get; set; }
}
public class Number{
    public string Number { get; set; }
    //... other logic, getter wrappers, etc. ...
}

但 EF 会将其解释为 "Number is a new type with a separate table, and the Contact object should have a FK to a row in the Number table"。我不想要 - 我想保持数据库模型不变。我可以在我的 C# 中手动进行转换,但最终会有很多样板。

我希望能够告诉 EF,“我知道这看起来像一个对象,但请相信我,它是一个原始数据。当您将它存储在数据库中时,它实际上是一个原始列。请转换每当您与数据库交互时,它都以适当的方式(如有必要,由数字 class 提供)往返于基元。

EF中有这个概念吗?

如果是这样,查找此文档的正确搜索词是什么?

有兴趣了解 EF 或 EFCore。

答案,由 Ivan Stoev 提供(请参阅问题评论):

Looks like the search term you are looking for is "Complex Type" in EF6.

示例搜索结果:https://entityframework.net/complex-type

In EFCore, a similar (but not identical) concept is "Owned Entity Type".

示例搜索结果:https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities