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表,其建 ...
随机推荐
- Java入门 - 语言基础 - 18.正则表达式
原文地址:http://www.work100.net/training/java-regular-expression.html 更多教程:光束云 - 免费课程 正则表达式 序号 文内章节 视频 1 ...
- windows上apache配置php5
windows上apache配置php5 重点:1.php5里的php.ini的extension_dir要改为绝对目录(带'/'斜杠),如果只是写个ext,在apache+mod_php里面是不会加 ...
- NOI2.5 1253:Dungeon Master
描述 You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of ...
- Deep server from scratch
Deep server from scratch 1.install Ubuntu16.04 via flash2.wired Network by Ruijie3.install google4.S ...
- 使用c++标准IO库实现txt文本文件的读与写操作
练习c++primer中关于输入输出流的操作. 任务是从固定格式的forreading.txt文档中读取相应的数据,转存到forwriting.txt中去. forreading.txt 格式如下: ...
- Springboot | @RequestBody 接收到的参数对象属性为空
背景 今天在调试项目的时候遇到一个坑,用Postman发送一个post请求,在Springboot项目使用@RequestBody接收时参数总是报不存在,但是多次检查postman上的请求格式以及项目 ...
- react中,路由的使用。import {BrowserRouter,Switch,Route} from "react-router-dom";
import React from "react"; import ReactDom from "react-dom"; import {BrowserR ...
- 4..部署场景2:带有遗留的Linux Bridge
此场景描述了使用Linux bridge的ML2插件实现OpenStack网络服务的遗留(基本)实现. 遗留实现通过为常规(非特权)用户提供一种方法来管理一个项目中的虚拟网络,并包含以下组件:提供了自 ...
- java-zhisji
1. int indexOf(int ch):用来检查给定的一个字符在当前字符串中第一次出现的下标位置.这里的下标和数组的下标意思相近,0表示该字符串的第1个字符,以此类推.当该字符串中并不 ...
- Codeforces_734_F
http://codeforces.com/problemset/problem/734/F x|y + x&y = x+y. #include<iostream> #includ ...