不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx、supervisor、mysql环境搭建搭建好了.net core linux的相关环境,今天就来说说ef core相关的配置及迁移:

简介:

Entity Framework(以下简称EF) 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案,EF Core是Entity framework得下一版本,相比EF原来版本(4-6.1),显得更加轻量级,相比同样出身名门的dapper还是显得有点庞大,不过鱼与熊掌向来不可兼得,想想一堆堆语法糖,方便的linq查询以及lambda表达式,可以很大程度上从T-SQL语法上脱身出来很多,在配置过程中也碰到一些问题,记录下来,以便后期翻阅:

一.poco的创建以及Fluent Api关系配置

1.一对多关系:

项目中最常见的就是一对多关系了,以语言及语言为例

语言(Language)类

 public partial class Language : BaseEntity<int>
{
private ICollection<LocaleStringResource> _localeStringResources; /// <summary>
/// Gets or sets the name
/// </summary> public string Name { get; set; } /// <summary>
/// Gets or sets the language culture
/// </summary> public string LanguageCulture { get; set; } /// <summary>
/// Gets or sets the unique SEO code
/// </summary> public string UniqueSeoCode { get; set; } /// <summary>
/// Gets or sets the flag image file name
/// </summary> public string FlagImageFileName { get; set; } /// <summary>
/// Gets or sets a value indicating whether the language supports "Right-to-left"
/// </summary> public bool Rtl { get; set; } /// <summary>
/// Gets or sets a value indicating whether the language is published
/// </summary> public bool Published { get; set; } /// <summary>
/// Gets or sets the display order
/// </summary> public int DisplayOrder { get; set; } public bool IsDefault { get; set; } /// <summary>
/// Gets or sets locale string resources
/// </summary>
public virtual ICollection<LocaleStringResource> LocaleStringResources
{
get { return _localeStringResources ?? (_localeStringResources = new HashSet<LocaleStringResource>()); }
protected set { _localeStringResources = value; }
}

语言资源(LocaleStringResource):

 public partial class LocaleStringResource : BaseEntity<int>
{
/// <summary>
/// Gets or sets the language identifier
/// </summary>
public int LanguageId { get; set; } /// <summary>
/// Gets or sets the resource name
/// </summary>
public string ResourceName { get; set; } /// <summary>
/// Gets or sets the resource value
/// </summary>
public string ResourceValue { get; set; } /// <summary>
/// Gets or sets a value indicating whether this resource was installed by a plugin
/// </summary>
public bool? IsFromPlugin { get; set; } /// <summary>
/// Gets or sets a value indicating whether this resource was modified by the user
/// </summary>
public bool? IsTouched { get; set; } /// <summary>
/// Gets or sets the language
/// </summary>
public virtual Language Language { get; set; } }

其中语言及资源主外键关系配置如下

  public partial class LanguageMapper : IEntityTypeConfiguration<Language>
{
public void Configure(EntityTypeBuilder<Language> builder)
{
builder.ToTable("Language");
builder.HasKey(r => r.Id);
builder.HasIndex(r => r.LanguageCulture);
builder.HasIndex(r => r.Name);
builder.HasIndex(r => r.UniqueSeoCode);
}
}
 public partial class LocaleStringResourceMapper : IEntityTypeConfiguration<LocaleStringResource>
{
public void Configure(EntityTypeBuilder<LocaleStringResource> builder)
{
builder.ToTable("LocaleStringResource");
builder.HasKey(r => r.Id);
builder.HasIndex(r => r.LanguageId);
builder.HasIndex(r => r.ResourceName);
builder.HasOne(r => r.Language).WithMany(b => b.LocaleStringResources)
.HasForeignKey(fk => fk.LanguageId);
}
}

2、父子关系 :

  public class TechCategory:BaseEntity<int>
{
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 分类描述
/// </summary>
public string Descript { get; set; }
/// <summary>
/// meta标题
/// </summary>
public string MetaTitle { get; set; }
/// <summary>
/// meta描述
/// </summary>
public string MetaDescript { get; set; }
/// <summary>
/// 缩略图
/// </summary>
public string Thumb { get; set; }
/// <summary>
/// 图片
/// </summary>
public string Image { get; set; }
/// <summary>
/// 排序
/// </summary>
public int Sort { get; set; }
/// <summary>
/// 父级分类
/// </summary>
public int? ParentId { get; set; } /// <summary>
/// 父级分类
/// </summary> public virtual TechCategory Parent { get; set; }
public virtual ICollection<Technology> Technologys { get; set; }
/// <summary>
/// 子级
/// </summary>
public virtual ICollection<TechCategory> Childs { get; set; }
/// <summary>
/// 关键词
/// </summary>
public string Slug { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public long CreatedOn { get; set; }
}
  public class TechCategoryMapper : IEntityTypeConfiguration<TechCategory>
{
public void Configure(EntityTypeBuilder<TechCategory> builder)
{
builder.ToTable("TechCategory");
builder.HasKey(r => r.Id);
builder.HasMany(r => r.Childs).WithOne(x => x.Parent)
.HasForeignKey(fk => fk.ParentId);
builder.HasIndex(r => r.Name);
}
}

二、数据库上下文:

根据前面创建的相关实体类 以及相关mapper类型,我们可以重现DbContext的OnModelCreating的方法,反射出实现了IEntityTypeConfiguration接口的相关mapper类型创建相关实体-数据表的相关对应关系

  public class DefaultContext : DbContext
{
public DefaultContext(DbContextOptions<DefaultContext> opt)
:base(opt)
{
AutoCommitEnabled = true;
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
var mappingInterface = typeof(IEntityTypeConfiguration<>);
// Types that do entity mapping
var mappingTypes = GetType().GetTypeInfo().Assembly.GetTypes()
.Where(x => x.GetInterfaces().Any(y => y.GetTypeInfo().IsGenericType && y.GetGenericTypeDefinition() == mappingInterface)); // Get the generic Entity method of the ModelBuilder type
var entityMethod = typeof(ModelBuilder).GetMethods()
.Single(x => x.Name == "Entity" &&
x.IsGenericMethod &&
x.ReturnType.Name == "EntityTypeBuilder`1"); foreach (var mappingType in mappingTypes)
{ // Get the type of entity to be mapped
var genericTypeArg = mappingType.GetInterfaces().Single().GenericTypeArguments.Single(); // Get the method builder.Entity<TEntity>
var genericEntityMethod = entityMethod.MakeGenericMethod(genericTypeArg); // Invoke builder.Entity<TEntity> to get a builder for the entity to be mapped
var entityBuilder = genericEntityMethod.Invoke(builder, null); // Create the mapping type and do the mapping
var mapper = Activator.CreateInstance(mappingType);
mapper.GetType().GetMethod("Configure").Invoke(mapper, new[] { entityBuilder });
}
foreach (var relationship in builder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
}
}

三、多数据库支持:

多数据库的支持,并不是意味着同时对多种数据库操作,当然,后面,我们会尝试同时对多种数据库操作,这可能需要多个上下文,暂且不论。分布式数据库。我们的项目可能是在windows上开发的使用的是SqlServer,我们要发布到linux上,SqlServer 2017 据说是支持liunx的,但是还没出... 当然不是说 SqlServer 就不能装在liunx上,但是我们的Liunx服务器可能已经安装了MySql或 Oracle,我们希望使用现有的,又或者是,我们需要切换数据库。那么,我们需要可以随时切换数据库的支持,以上篇的mysql为例:

1.引用相关包,mysql我用的Pomelo.EntityFrameworkCore.MySql,国人开发的mysql for ef core的相关服务对象,ef

core 2.0中需要指定2.0版本(Install-Package Pomelo.EntityFrameworkCore.MySql -Version 2.0.0-rtm-10059),

2.需改配置文件依据相关配置动态加载数据库提供对象;相关代码及配置如下

nuget引用列表

  <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.0.0-rtm-10058" />
</ItemGroup>

相关配置文件修改

 "ConnectionStrings": {
"MongoDbConnectionString": "mongodb://tchistory:tc123456@127.0.0.1/history",
"ConnectionString": "Data Source=127.0.0.1;Initial Catalog=farmdata;User ID=root;Password=123456",
"DataProvider": "MySql"
},

在startup.cs 的configureServices方法中添加数据库上下文

        public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DefaultContext>(option => option.UseFarmDatabase(Configuration));
services.UseFarmService(Configuration);
services.AddMvc(); }

数据库提供对象扩展方法如下:

public static DbContextOptionsBuilder UseFarmDatabase(this DbContextOptionsBuilder optionsBuilder, IConfiguration configuration)
{
string provider = configuration.GetConnectionString("DataProvider"), connection = configuration.GetConnectionString("ConnectionString");
if (provider.Equals(DataBaseServer.SqlServer, StringComparison.InvariantCultureIgnoreCase))
{
return optionsBuilder.UseSqlServer(connection);
}
else if (provider.Equals(DataBaseServer.MySql, StringComparison.InvariantCultureIgnoreCase))
{
return optionsBuilder.UseMySql(connection);
}
else
{
throw Error.Argument("UseDatabaseServer", "No databaseProvider");
}
}

在程序包管理控制台添加相关迁移 命令 add-migration init:  会在当前目录下生成相关迁移目录

执行update-database -Verbose提交更改到数据库,这样数据库迁移及更改就算完成了,其中要注意的地方是ef core 2.0 中有些数据库提供对象暂时是没有完全实现,如用Pomelo.EntityFrameworkCore.MySql -Version 1.1.2 可能会报方法未实现,更新2.0 rtm版本即可。

祝愿你在码农道路上越走越顺畅;

一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移的更多相关文章

  1. ASP.NET Core Web开发学习笔记-1介绍篇

    ASP.NET Core Web开发学习笔记-1介绍篇 给大家说声报歉,从2012年个人情感破裂的那一天,本人的51CTO,CnBlogs,Csdn,QQ,Weboo就再也没有更新过.踏实的生活(曾辞 ...

  2. ASP.NET Core快速入门--学习笔记系列文章索引目录

    课程链接:http://video.jessetalk.cn/course/explore 良心课程,大家一起来学习哈! 抓住国庆假期的尾巴完成了此系列课程的学习笔记输出! ASP.NET Core快 ...

  3. ASP.NET Core MVC 网站学习笔记

    ASP.NET Core MVC 网站学习笔记 魏刘宏 2020 年 2 月 17 日 最近因为” 新冠” 疫情在家办公,学习了 ASP.NET Core MVC 网站的一些知识,记录如下. 一.新建 ...

  4. python3.4学习笔记(二十五) Python 调用mysql redis实例代码

    python3.4学习笔记(二十五) Python 调用mysql redis实例代码 #coding: utf-8 __author__ = 'zdz8207' #python2.7 import ...

  5. ASP.NET Core快速入门学习笔记(第3章:依赖注入)

    课程链接:http://video.jessetalk.cn/course/explore 良心课程,大家一起来学习哈! 任务16:介绍 1.依赖注入概念详解 从UML和软件建模来理解 从单元测试来理 ...

  6. ASP.NET Core快速入门学习笔记(第2章:配置管理)

    课程链接:http://video.jessetalk.cn/course/explore 良心课程,大家一起来学习哈! 任务9:配置介绍 命令行配置 Json文件配置 从配置文件文本到c#对象实例的 ...

  7. ASP.NET Core快速入门学习笔记(第1章:介绍与引入)

    课程链接:http://video.jessetalk.cn/course/explore 良心课程,大家一起来学习哈! 任务1:课程介绍 任务2:环境安装 下载地址:https://dotnet.m ...

  8. gRPC在 ASP.NET Core 中应用学习(二)

    前言: 上一篇文章中简单的对gRPC进行了简单了解,并实现了gRPC在ASP.NET Core中服务实现.客户端调用:那么本篇继续对gRPC的4中服务方法定义.其他使用注意点进一步了解学习 一.gRP ...

  9. ASP.NET Core框架深度学习(二) 管道对象

    4.HttpContext 第一个对象 我们的ASP.NET Core Mini由7个核心对象构建而成.第一个就是大家非常熟悉的HttpContext对象,它可以说是ASP.NET Core应用开发中 ...

随机推荐

  1. C# 设计模式之空对象模式

    最近看了不少的书籍和视频等相关资料,决定自己边学习边写一下个人对设计模式的理解,如果有不对的请大家多多指正. 今天先说说我个人觉得最简单的设计模式 -- [空对象模式] 空对象模式可以减少客户端对对象 ...

  2. Week 1 # A A + B Problem II

    原题描述: A - A + B Problem II I have a very simple problem for you. Given two integers A and B, your jo ...

  3. SVN常见问题

    one or more files are in a conflicted state.(一个或多个文件处于矛盾状态)意思是这个文件已经被其他人修改过了. 然后我点击ok按钮后,找到冲突的文件再次up ...

  4. (转) 使用jdk的xjc命令由schema文件生成相应的实体类

    背景:在webservice的开发过程中涉及到这一知识点,又必要来学习一下. 1 根据编写的schema来生成对应的java实体 1.1 实战 xcj命令有schema文件生成Java实体类 1.使用 ...

  5. opencv+python3.4的人脸识别----2017-7-19

    opencv3.1  +  python3.4 第一回合(抄代码,可实现):人脸识别涉及一个级联表,目前能力还无法理解. 流程:1.读取图像---2.转换为灰度图---3.创建级联表---4.对灰度图 ...

  6. 函数响应式编程及ReactiveObjC学习笔记 (四)

    今天我们继续看其他的类别 UIImagePickerController+RACSignalSupport.h #import <UIKit/UIKit.h> @class RACDele ...

  7. JS浏览器对象:window对象、History、Location对象、Screen对象

    一.JS浏览器对象-window 1.window对象 window对象是BOM的核心,window对象指当前的浏览器窗口 所有JavaScript全局对象.函数以及变量均自动成为window对象的成 ...

  8. 模拟EF CodeFist 实现自己的ORM

    一.什么是ORM 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单来说,ORM 是通过使用描述对象 ...

  9. Swift学习之元组(Tuple)

    定义 元组是由若干个类型的数据组成,组成元组的数据叫做元素,每个元素的类型都可以是任意的. 用法一 let tuples1 = ("Hello", "World" ...

  10. Python操作文件和目录

    Python操作文件和目录 读写文件比较简单,有一点特别注意就好了 windows下Python默认打开的文件以gbk解码,而一般我们的文件是utf-8编码的,所以如果文本含有中文,就会出现异常或者乱 ...