EFCore 2.0的IEntityTypeConfiguration<TEntity>的使用!
通过新建一个类来实现 IEntityTypeConfiguration 这个接口,将EFCore中的实体配置写在单独的配置类中,便于修改和维护。
OnModelCreating代码:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); modelBuilder.Entity<Class>().ToTable("T_Classs"); modelBuilder.Entity<Teachers>().ToTable("T_Teachers"); //modelBuilder.Entity<Students>() //这是EF 2.0之前版本将配置写在OnModelCreating方法中的写法
// .ToTable("T_Students")
// .HasOne(s => s.Class)
// .WithMany(e => e.Students)
// .HasForeignKey(e => e.ClassId); modelBuilder.ApplyConfiguration(new StudentCofig()); //这是将单独的配置类注册到OnModelCreating中 modelBuilder.ApplyConfiguration(new TeacherClassConfig()); }
新建的实体配置类:
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using MyEF2.Models;
using System;
using System.Collections.Generic;
using System.Text; namespace MyEF2.Configuration
{
public class StudentCofig : IEntityTypeConfiguration<Students> //继承该接口
{
public void Configure(EntityTypeBuilder<Students> builder)
{
builder.ToTable("T_Students")
.HasOne(s => s.Class)
.WithMany(e => e.Students)
.HasForeignKey(e => e.ClassId);
}
}
}
最后在MyDbContext中的OnModelCreating方法中注册:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); modelBuilder.Entity<Class>().ToTable("T_Classs"); modelBuilder.Entity<Teachers>().ToTable("T_Teachers"); modelBuilder.ApplyConfiguration(new StudentCofig()); //这是将单独的配置类注册到OnModelCreating中 modelBuilder.ApplyConfiguration(new TeacherClassConfig()); }
EFCore 2.0的IEntityTypeConfiguration<TEntity>的使用!的更多相关文章
- EFCore 6.0入门看这篇就够了
前言 作为一直在dotNet行业耕耘的码农,这几年在大大小小项目中也涉及到了许多ORM框架,比如:EFCore,Dapper,NHibernate,SqlSugar等等,这些ORM都有各自的优缺点,大 ...
- EFCore 2.0引用标量函数
参考文档:https://www.cnblogs.com/CreateMyself/p/8485697.html 1.添加nuget包:EntityFramework.Functions,在上下文类M ...
- Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- 基于EFCore的数据Cache实现
.NetCore 内置缓存加入到EFCore操作中,数据更新或者查询时自动更新缓存.github地址 2019-04-27 初步完成逻辑代码编写,尚未经过测试,诸多细节有待完善. 2019-04-28 ...
- Entity Framework Core 2.0 全局查询过滤器
不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址 本博文翻译自: http://gunnarpeipman.com/2017/08/ef ...
- Entity Framework Core 2.0 入门简介
不多说废话了, 直接切入正题. EF Core支持情况 EF Core的数据库Providers: 此外还即将支持CosmosDB和 Oracle. EFCore 2.0新的东西: 查询: EF.Fu ...
- Entity Framework Core 2.0 入门
该文章比较基础, 不多说废话了, 直接切入正题. 该文分以下几点: 创建Model和数据库 使用Model与数据库交互 查询和保存关联数据 EF Core支持情况 EF Core的数据库Provide ...
- ZKWeb网页框架3.0正式发布
3.0 更新的内容有 更新 .NET 框架 替换项目模版的 netcoreapp2.2 到 netcoreapp3.0 目前支持的 .NET 框架有: net461, netcoreapp2.0, n ...
- EntityFrameworkCore数据迁移(一)
.net core出来已经有很长一段时间了,而EentityFrameworkCore(后面简称EFCore)是.net framework的EntityFramework在.net core中的实现 ...
随机推荐
- Docker 清理日志
docker 长时间运行后,日志文件会逐渐变大可以使用下面命令进行清除 #!/bin/bash echo "==================== start clean docker c ...
- 【MongoDB详细使用教程】三、高级查询
目录 1.使用比较运算符查询 2.使用关键字查询 2.1.in/not in 关键字 2.2.size 关键字 2.3.exists 关键字 2.4.or 关键字 3.模糊查询 4.查询结果排序 5. ...
- liteos软件定时器(十)
1 概述 1.1 基本概念 软件定时器,是基于系统Tick时钟中断且由软件来模拟的定时器,当经过设定的Tick时钟计数值后会触发用户定义的回调函数.定时精度与系统Tick时钟的周期有关. 硬件定时器受 ...
- windows经典主题 桌面颜色(R58 G110 U165)
- 便宜的回文 (USACO 2007)(c++)
2019-08-21便宜的回文(USACO 2007) 内存限制:128 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 题目描述 追踪每头奶牛的去向是一件棘手的任 ...
- 浅谈SOA与RPC
一.SOA 英文名称:Service Oriented Ambiguity 中文名称:面向服务架构 SOA是一种思想,目的是提供一种设计项目的思路,让开发时更有效率. 例如原来的分布式项目中,在每个项 ...
- django之choice、ajax初步
django之choice参数,ajax choice参数 应用场景:主要是用户性别.用户工作状态.成绩对应 ##在测试文件中运行,需要写以下几个模块 if __name__ == "__m ...
- Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4) 题解
Happy Birthday, Polycarp! Make Them Odd As Simple as One and Two Let's Play the Words? Two Fairs Bea ...
- <String> 345 205
345. Reverse Vowels of a String 头尾指针开始扫描. String.contains(char) class Solution { public String rever ...
- 【转】hibernate对象三种状态
hibernate里对象有三种状态: 1,Transient 瞬时 :对象刚new出来,还没设id,设了其他值. 2,Persistent 持久:调用了save().saveOrUpdate(),就变 ...