EF CORE中复杂类型的映射
实体映射时,遇到复杂类型,可选择下述方法处理:
- NotMapped,跳过映射
- 在复杂类型上声明 [Owned],但仅限该复杂类型是全部由简单值类型组成的
- 自定义序列化方法
示例: IPInfo使用了owned,对IPEndPoint使用自定义序列化,对VersionInfo使用JSON序列化
@@@code
public
class
Controller : IController
{
public
int SN { get; set; }
public IPInfo IPInfo { get; set; } = IPInfo.Default;
[Column(TypeName = "string")]
public VersionInfo VersionInfo { get; set; } = VersionInfo.Default;
[Column(TypeName = "string")]
public System.Net.IPEndPoint ServerIPEndPoint { get; set; } = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
public DateTime Time { get; set; } = DateTime.Now;
} [Owned]
public
class
IPInfo
{
public
static IPInfo Default { get; } = new IPInfo()
{
IP="192.168.0.254"
};
public
string IP { get; set; } public
ushort Port { get; set; } = 60000;
public
string Mac { get; set; }
public
string Mask { get; set; } = "255.255.255.0";
public
string Gateway { get; set; } = "192.168.0.1";
public
bool Force { get; set; } }
@@#
自定义序列化
@@@code public
class
IPEndPointConverter : ValueConverter<System.Net.IPEndPoint, string>
{
public
IPEndPointConverter(ConverterMappingHints mappingHints = null)
: base(
v => v.ToString(),
v => System.Net.IPEndPoint.Parse(v),
mappingHints)
{
} public
static ValueConverterInfo DefaultInfo { get; }
= new ValueConverterInfo(typeof(System.Net.IPEndPoint), typeof(string), i => new IPEndPointConverter(i.MappingHints));
}
public
class
JsonConverter<T> : ValueConverter<T, string>
{
public
JsonConverter() : this(null)
{ }
public
JsonConverter(ConverterMappingHints mappingHints = null)
: base(
v => v.SerializeObject(),
v => v.Deserialize<T>(),
mappingHints)
{
} public
static ValueConverterInfo DefaultInfo { get; }
= new ValueConverterInfo(typeof(T), typeof(string), i => new JsonConverter<T>(i.MappingHints));
} protected
override
void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
void aa<T>() where T : class
{
modelBuilder.Entity<T>().ToTable(typeof(T).Name.ToLower());
}
aa<User>();
aa<Device>(); foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{ foreach (var property in entityType.GetProperties())
{
if (property.ClrType.IsValueType && !property.ClrType.IsGenericType)
continue; switch (property.ClrType.Name)
{
case nameof(System.Net.IPEndPoint):
property.SetValueConverter(new IPEndPointConverter()); //演示 owned效果,仅限复杂类型是由简单类型组成的,没有内嵌复杂类型
break;
case nameof(String):
break;
default:
Type genType = typeof(JsonConverter<>).MakeGenericType(property.ClrType);
ValueConverter obj = Activator.CreateInstance(genType) as ValueConverter;
property.SetValueConverter(obj);
break;
} }
} }
@@#

EF CORE中复杂类型的映射的更多相关文章
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
项目开发中的一些注意事项以及技巧总结 1.jquery采用ajax向后端请求时,MVC框架并不能返回View的数据,也就是一般我们使用View().PartialView()等,只能返回json以 ...
- EF Core中的多对多映射如何实现?
EF 6.X中的多对多映射是直接使用HasMany-HasMany来做的.但是到了EF Core中,不再直接支持这种方式了,可以是可以使用,但是不推荐,具体使用可以参考<你必须掌握的Entity ...
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...
- EF Core 中DbContext不会跟踪聚合方法和Join方法返回的结果,及FromSql方法使用讲解
EF Core中: 如果调用Queryable.Count等聚合方法,不会导致DbContext跟踪(track)任何实体. 此外调用Queryable.Join方法返回的匿名类型也不会被DbCont ...
- EF Core中通过Fluent API完成对表的配置
EF Core中通过Fluent API完成对表的配置 设置实体在数据库中的表名 通过ToTable可以为数据模型在数据库中自定义表名,如果不配置,则表名为模型名的复数形式 public class ...
- [小技巧]EF Core中如何获取上下文中操作过的实体
原文地址:https://www.cnblogs.com/lwqlun/p/10576443.html 作者:Lamond Lu 源代码:https://github.com/lamondlu/EFC ...
- EF Core中避免贫血模型的三种行之有效的方法(翻译)
Paul Hiles: 3 ways to avoid an anemic domain model in EF Core 1.引言 在使用ORM中(比如Entity Framework)贫血领域模型 ...
- 9.4 翻译系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
原文链接:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-f ...
- EF Core 中多次从数据库查询实体数据,DbContext跟踪实体的情况
使用EF Core时,如果多次从数据库中查询一个表的同一行数据,DbContext中跟踪(track)的实体到底有几个呢?我们下面就分情况讨论下. 数据库 首先我们的数据库中有一个Person表,其建 ...
随机推荐
- V模型
V模型是Kevin Forsberg & Harold Mooz在1978年提出的,V模型强调测试在系统工程各个阶段中的作用,并将系统分解和系统集成的过程通过测试彼此关联.V模型从整体上看起来 ...
- Exception in thread "main" java.lang.AbstractMethodError: org.springframework.boot.context.config.ConfigFileApplicationListener.supportsSourceType(Ljava/lang/Class;)Z
依赖冲突,查看pom.xml文件 查看parent项目的依赖版本为 <parent> <groupId>org.springframework.boot</groupId ...
- Myeclipse 2017 下载+安装+激活+集成配置【JRE 8+Tomcat 9+MySQL 5.7.29】
Myeclipse 2017 的下载 Myeclipse 2017 下载地址:https://www.jianguoyun.com/p/DTEBo1cQ6LnsBxj9984C Myeclipse 2 ...
- 基于python的感知机
一. 1.感知机可以描述为一个线性方程,用python的伪代码可表示为: sum(weight_i * x_i) + bias -> activation #activation表示激活函数,x ...
- Halo-个人独立博客系统
项目地址:https://github.com/halo-dev/halo 安装指导:https://halo.run/guide/ 简介: Halo 是一款现代化的个人独立博客系统,给习惯写博客 ...
- C#服务端的GET、POST请求
一.HttpClient方式,程序集 System.Net.Http.dll GET: HttpClient httpClient = new HttpClient(); string result ...
- 富文本编辑器、全文检索和django发送邮件
1.富文本编辑器 1.1快速了解 借助富文本编辑器,网站的编辑人员能够像使用offfice一样编写出漂亮的.所见即所得的页面.此处以tinymce为例,其它富文本编辑器的使用也是类似的. 在虚拟环境中 ...
- Centos 7 最小化部署zabbix
前言 文章内容是作者本人编写,之前一直放在word文档中,突然有闲情转移到博客上来了,欢迎后续观看者有问题找我探讨~~~ 废话不多说,先说下原理吧 概述 工作原理 通过c/s模式采集数据,基于b/s模 ...
- Vertx使用EventBus发送接受自定义对象
先看官方文档步骤: 需要一个编解码器,看源码: 可见内置了需要数据类型的实现,所以发送其他消息可以发送,但是如果发送自定义对象就需要自己实现编解码逻辑了 一 自定义编解码器 /** * 自定义对象编解 ...
- C# 解析JSON遇到以错误提示:应为状态“Element”。。遇到名称为“”、命名空间为“”的“Text”。
话不多说:仔细看代码: ①json格式错误导致报错 {"TeachIQ":" 语言 0小时0分钟 未完成","Temperature" ...