- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
假设我有一个 EF 实体类 Person,上面有一个 PhoneNumber。 PhoneNumber 存储为字符串类型,但我希望对 Person 的所有访问都通过具有一些不错的访问器函数的 Phone,例如验证或 GetAreaCode()
。我想在数据库中将其作为字符串备份,但在查询时我想将其作为电话号码返回:
public class Person {
public PhoneNumber Phone { /* Some clever get/set logic here */ }
private string _phoneNumber; // Backing field
}
或者我可以让 PhoneNumber 将其自身存储为字符串吗?如果我只是通过删除上面的支持字段将其包含在模型中,EF 会被构造函数(一个 protected 构造函数,其参数比一个字符串更多)以及一个复制构造函数 PhoneNumber(PhoneNumber other)
。我可以让 EF 以某种方式忽略那些吗?
我对想法持开放态度...
最佳答案
您可以使用@nbrosz 的answer来解决您的问题,但如果您使用的是 EF Core 2.1,则不再需要执行此类解决方法。您可以使用 EF Core 2.1(自 2018 年 5 月 7 日起在 Release Candidate 1 中)删除支持字段,您可以使用值转换功能解释 here由微软:
Value converters allow property values to be converted when reading from or writing to the database. This conversion can be from one value to another of the same type (for example, encrypting strings) or from a value of one type to a value of another type (for example, converting enum values to and from strings in the database.)
因此对于您的情况,您可以删除支持字段。你不再需要它了。你的类应该是这样的:
public class Person
{
public PhoneNumber Phone { /* Some clever get/set logic here */ }
}
在你的OnModelCreating
方法,您可以像下面这样配置转换:
modelBuilder.Entity<Person>()
.Property(p => p.Phone)
.HasConversion(
phone => {
// Here you code the logic of how to get the value to store in DB
return ...;
},
dbValue => {
// Here you code the logic of how to construct the PhoneNumber instance from the value to store in DB
}
);
就是这样。实际上它在候选版本中,但微软说:
EF Core 2.1 RC1 is a “go live” release, which means once you test that your application works correctly with RC1, you can use it in production and obtain support from Microsoft, but you should still update to the final stable release once it’s available.
我回答的其余部分是针对 @nbrosz 的,因为您正在处理枚举类型。您可以删除支持字段,也可以删除 EF Core 2.1 提供的众多内置值转换器之一。对于枚举到字符串值的转换,我们有类型 EnumToStringConverter
.对于您在回答中所做的逻辑,您可以将其简化为实体:
[Display(Name = "Fire Type")]
public Enums.FireType Type { get; set; }
我们删除了 NotMapped
属性上的属性,没有用于转换的逻辑 y。
在你的OnModelCreating
你这样做的方法:
var converter = new EnumToStringConverter<FireType>();
modelBuilder
.Entity<Fire>()
.Property(e => e.FireType)
.HasConversion(converter);
您还可以让 EF Core 通过使用 HasConversion<T>
的通用版本为您检测正确的转换器如下所示:
modelBuilder
.Entity<Fire>()
.Property(e => e.FireType)
.HasConversion<string>();
如果你不喜欢使用流畅的配置,你可以使用 Column
如下所示的数据注释属性,EF Core 将为您进行转换:
[Column(TypeName = "nvarchar(20)")]
[Display(Name = "Fire Type")]
public Enums.FireType Type { get; set; }
关于c# - EF Core 支持字段 - 将属性公开为另一种类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49193292/
EF POCO 和 EF Code First 有什么区别? 如果我一开始只使用 POCO,我可以将 EF 与它们一起使用吗? 最佳答案 如果您首先使用 EF 代码,您将拥有 POCO 对象,并且数据
EF POCO 和 EF Code First 有什么区别? 如果我一开始只使用 POCO,我可以将 EF 与它们一起使用吗? 最佳答案 如果您首先使用 EF 代码,您将拥有 POCO 对象,并且数据
我有一个基于 .NET 4.8 和 EF 6.4.4 的项目。我们正在逐步迁移到 .Net Core,但在此过程中我可以创建一个 .NET Core 数据上下文类 EF Core 并将两者指向相同的实
我有以下 Entity Framework 5 代码第一类 public class Airplane { public int Id { get; set; } public int
我正在尝试使用 Entity Framework Core 对现有数据库进行逆向工程.我试着按照指示from Microsoft但我遇到了错误: Unable to find provider ass
当数据库不是由 EF 代码首先创建时,有没有办法检查 DbContext 是否与数据库匹配? 我正在寻找与 Database.CompatibleWithModel 类似的功能但没有元数据。 最佳答案
目前,我正在重构我的上下文方法的测试,以不再需要真正的数据库。我使用 Ef Core。 所以我通读了 Microsoft 文档如何测试上下文方法。我首先找到了 EF6 测试的文档,然后阅读了 EfCo
我正在使用 EF 6 Database.SqlQuery 语句来执行存储过程,并实现错误和事务处理,打开和关闭事务(处理多个记录,如果一个记录有错误,仅回滚此,并提交其他任何内容无错误完成)。 Lis
我首先使用 EF 数据库,因为我喜欢在 SQL Management Studio 中设计我的数据库,坦率地说,让 Visual Studio 直接从数据库创建所有实体非常容易,而无需执行任何代码。
我的项目中有几个迁移文件,由于我对上次迁移进行了手动修改,所以我不想使用“程序包管理器控制台”重新生成它。我只需要添加 1 列。所以在之前的迁移中手动添加了这个(我可以这样做,因为还没有人升级过)。
原文:https://bit.ly/2umidlb 作者:jon p smith 翻译:王亮 声明:我翻译技术文章不是逐句翻译的,而是根据我自己的理解来表述的。其中可能会去除一些本人实在不知道如何组
我们想开始一个新项目,我们决定从一开始就在一些表中使用 Columnstore 索引和聚簇索引,我们如何使用 Code First EF Core 3.1 做到这一点? 最佳答案 您应该将主键更改为非
我在 Entity Framework 6.0 上。这是一个开发问题,而不是生产问题。 我想我有一个相互矛盾的策略。 目前,我设置了 DropCreateDatabaseIfModelChanges
我在 VS 2012 RTM 上,使用 EF 5。我在做代码优先,但由于我只是在开发中,所以试图忽略代码迁移。为了避免它们,我有这一套 Database.SetInitializer(new Drop
我有复杂的审计字段类型 我的复杂类型: [ComplexType] public class AuditData { [Column("CreatorUserId")] public
我已经阅读了很多关于如何在关系中指定外键名称的帖子,没有遇到任何问题。不过,我想知道的是,有没有办法更改主键和关系的命名方式? 例如,您有一个以 UserId 作为主键的 User 表。 EF 代码第
我刚刚安装了新的Entity Framework 4.1 NuGet包,因此根据NuGet指令和this article of Scott Hanselman替换了EFCodeFirst包。 现在,想
我的应用程序基于 .NET 4.0 和 EF 4。我现在正在考虑升级到最新版本。 是否有任何可能对我的申请产生不利影响的重大变化或行为差异? 升级路径有多简单?升级到 EF 5 是否需要任何代码更改或
假设您必须使用 EF Code First 和 POCO 类开发支持多种语言的网站,您将如何对 POCO 类进行建模以支持这种情况? 通过多语言支持,我的意思不仅是拥有例如在一些资源文件中为您的 UI
我首先使用 EF 4.1 代码。鉴于以下类片段: public class Doctor { public virtual ICollection Hospitals { get; set;
我是一名优秀的程序员,十分优秀!