广告
返回顶部
首页 > 资讯 > 精选 >Entity Framework使用Fluent API配置的方法
  • 312
分享到

Entity Framework使用Fluent API配置的方法

2023-06-29 09:06:11 312人浏览 薄情痞子
摘要

本篇内容介绍了“Entity Framework使用Fluent api配置的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学

本篇内容介绍了“Entity Framework使用Fluent api配置的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、配置主键

要显式将某个属性设置为主键,可使用 HasKey 方法。在以下示例中,使用了 HasKey 方法对 Product 类型配置 ProductId 主键。

1、新加Product类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPI.Model{    public class Product    {        public int ProductId { get; set; }        public string ProductName { get; set; }        public decimal Price { get; set; }    }}

2、新建ProductMap类,用来设置主键

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.Entity;using System.Data.Entity.ModelConfiguration;using FluentAPI.Model;namespace FluentAPI.Data.FluentAPIMap{    public class ProductMap : EntityTypeConfiguration<Product>    {        public ProductMap()        {            //使用 HasKey 方法对 Product 类型配置 ProductId 主键。            this.HasKey(p => p.ProductId);        }    }}

3、查看数据库

Entity Framework使用Fluent API配置的方法

二、配置复合主键

以下示例配置要作为Department 类型的组合主键的DepartmentID 和 Name 属性。

1、创建Department类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPI.Model{    public class Department    {        public int DepartmentId { get; set; }        public string Name { get; set; }        public decimal Budget { get; set; }        public DateTime StartDate { get; set; }    }}

2、创建DepartmentMap类,用来设置复合主键

using FluentAPI.Model;using System;using System.Collections.Generic;using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPI.Data.FluentAPIMap{    public class DepartmentMap : EntityTypeConfiguration<Department>    {        public DepartmentMap()        {            // 使用匿名类的方式配置DepartmentId和Name作为复合主键            this.HasKey(p => new {p .DepartmentId,p.Name});        }    }}

3、查看数据库

使用EF的数据迁移,然后查看数据库表

Entity Framework使用Fluent API配置的方法

三、关闭数值主键的标识

数值主键的标识DatabaseGeneratedOption是一个枚举值,该枚举值具有下面三个值:

DatabaseGeneratedOption.None:关闭数值主键。
DatabaseGeneratedOption.Identity:设置数值主键 自动增长 ,
DatabaseGeneratedOption.Computed :数值主键的值由计算得到(此列将无法插入值)。

1、设置关闭数值主键

using FluentAPI.Model;using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPI.Data.FluentAPIMap{    public class DepartmentMap : EntityTypeConfiguration<Department>    {        public DepartmentMap()        {            // 使用匿名类的方式配置DepartmentId和Name作为复合主键            this.HasKey(p => new {p .DepartmentId,p.Name});            // 以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值不由数据库生成。            this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);        }    }}

2、插入数据库表的时候DepartmentId列要显示的指定值:

INSERT INTO Departments VALUES (1, '人事部',12.3,GETDATE());

四、指定属性的最大长度

HasMaxLength可以设置表中列的最大长度。

using FluentAPI.Model;using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPI.Data.FluentAPIMap{    public class DepartmentMap : EntityTypeConfiguration<Department>    {        public DepartmentMap()        {            // 使用匿名类的方式配置DepartmentId和Name作为复合主键            this.HasKey(p => new {p .DepartmentId,p.Name});            // 以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值不由数据库生成。            //this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);            //  以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值由数据库自动生成。            //this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);            //Name属性不应超过 50 个字符。如果其值超过 50 个字符,则出现 DbEntityValidationException 异常。            //如果 Code First 基于此模型创建数据库,它还会将 Name 列的最大长度设置为50 个字符。            this.Property(p => p.Name).HasMaxLength(50);        }    }}

五、将属性配置为必需

using FluentAPI.Model;using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPI.Data.FluentAPIMap{    public class DepartmentMap : EntityTypeConfiguration<Department>    {        public DepartmentMap()        {            // 使用匿名类的方式配置DepartmentId和Name作为复合主键            this.HasKey(p => new {p .DepartmentId,p.Name});            // 以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值不由数据库生成。            //this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);            //  以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值由数据库自动生成。            //this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);            //Name属性不应超过 50 个字符。如果其值超过 50 个字符,则出现 DbEntityValidationException 异常。            //如果 Code First 基于此模型创建数据库,它还会将 Name 列的最大长度设置为50 个字符。            this.Property(p => p.Name).HasMaxLength(50);                        this.Property(p => p.Name).IsRequired();        }    }}

六、指定不将CLR 属性映射到数据库中的列

using FluentAPI.Model;using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPI.Data.FluentAPIMap{    public class DepartmentMap : EntityTypeConfiguration<Department>    {        public DepartmentMap()        {            // 使用匿名类的方式配置DepartmentId和Name作为复合主键            this.HasKey(p => new {p .DepartmentId,p.Name});            // 以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值不由数据库生成。            //this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);            //  以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值由数据库自动生成。            //this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);            //Name属性不应超过 50 个字符。如果其值超过 50 个字符,则出现 DbEntityValidationException 异常。            //如果 Code First 基于此模型创建数据库,它还会将 Name 列的最大长度设置为50 个字符。            this.Property(p => p.Name).HasMaxLength(50);                        this.Property(p => p.Name).IsRequired();                        this.Ignore(p => p.Budget);        }    }}

七、将CLR 属性映射到数据库中的特定列

HasColumnName可以用来设置映射到数据库表中列的列名。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.Entity;using System.Data.Entity.ModelConfiguration;using FluentAPI.Model;using System.ComponentModel.DataAnnotations.Schema;namespace FluentAPI.Data.FluentAPIMap{    public class ProductMap : EntityTypeConfiguration<Product>    {        public ProductMap()        {            //使用 HasKey 方法对 Product 类型配置 ProductId 主键。            this.HasKey(p => p.ProductId);                        this.Property(p => p.Price).HasColumnName("ProductPrice");        }    }}

八、配置字符串属性是否支持Unicode 内容

IsUnicode()方法可以用来设置是否支持Unicode字符,该方法有两个重载函数。

1、没有参数的重载,默认支持Unicode字符

Entity Framework使用Fluent API配置的方法

2、有参数的重载,参数为bool值,true支持Unicode,false不支持Unicode

Entity Framework使用Fluent API配置的方法

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.Entity;using System.Data.Entity.ModelConfiguration;using FluentAPI.Model;using System.ComponentModel.DataAnnotations.Schema;namespace FluentAPI.Data.FluentAPIMap{    public class ProductMap : EntityTypeConfiguration<Product>    {        public ProductMap()        {            //使用 HasKey 方法对 Product 类型配置 ProductId 主键。            this.HasKey(p => p.ProductId);                        this.Property(p => p.Price).HasColumnName("ProductPrice");                        this.Property(p => p.PlaceOfOrigin).IsUnicode(false);        }    }}

查看数据库列类型:

Entity Framework使用Fluent API配置的方法

九、配置数据库列的数据类型

HasColumnType 方法支持映射到相同基本类型的不同表示。

this.Property(p => p.Name).HasColumnType("varchar");

十、配置复杂类型的属性

1、新建类Course,里面有一个Department类型的属性:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPIApp.Model{    public class Course    {        public int CourseID { get; set; }        public string Title { get; set; }        public int Credits { get; set; }        public virtual Department Department { get; set; }    }}
using FluentAPI.Model;using System;using System.Collections.Generic;using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPI.Data.FluentAPIMap{    public class CourseMap : EntityTypeConfiguration<Course>    {        public CourseMap()        {                        this.Property(p => p.Department.Name).HasMaxLength(32);        }    }}

十一、将CLR 实体类型映射到数据库中的特定表

ToTable("t_Department");ToTable("t_Department", "school");

“Entity Framework使用Fluent API配置的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!

--结束END--

本文标题: Entity Framework使用Fluent API配置的方法

本文链接: https://www.lsjlt.com/news/323969.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Entity Framework使用Fluent API配置的方法
    本篇内容介绍了“Entity Framework使用Fluent API配置的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学...
    99+
    2023-06-29
  • Entity Framework使用Fluent API配置案例
    一、配置主键 要显式将某个属性设置为主键,可使用 HasKey 方法。在以下示例中,使用了 HasKey 方法对 Product 类型配置 ProductId 主键。 1、新加Pro...
    99+
    2022-11-13
  • Entity Framework使用配置伙伴创建数据库
    在上一篇文章中讲了如何使用fluent API来创建数据表,不知道你有没有注意到一个问题。上面的OnModelCreating方法中,我们只配置了一个类Product,也许代码不是很...
    99+
    2022-11-13
  • Entity Framework Core延迟加载的方法怎么使用
    本文小编为大家详细介绍“Entity Framework Core延迟加载的方法怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Entity Framework Core延迟加载的方法怎么使用...
    99+
    2023-06-29
  • Entity Framework中怎么使用配置伙伴创建数据库
    这篇“Entity Framework中怎么使用配置伙伴创建数据库”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“E...
    99+
    2023-06-29
  • Vue 配置 GraphQL API的方法
    本篇内容介绍了“ Vue 配置 GraphQL API的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!...
    99+
    2022-10-19
  • Keycloak各种配置及API使用的方法是什么
    这篇文章主要介绍了Keycloak各种配置及API使用的方法是什么的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Keycloak各种配置及API使用的方法是什么文章都会有所收获,下面我们一起来看看吧。1.创建c...
    99+
    2023-07-05
  • Vue3使用axios的配置方法
    本篇内容主要讲解“Vue3使用axios的配置方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Vue3使用axios的配置方法”吧!一、安装axiosnpm install ...
    99+
    2023-06-30
  • SpringCloud Config使用配置方法
    Config 介绍 Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口...
    99+
    2022-11-12
  • mybatis spring配置SqlSessionTemplate的使用方法
    这篇文章主要讲解了“mybatis spring配置SqlSessionTemplate的使用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mybatis spring配置SqlSess...
    99+
    2023-06-20
  • VScode中配置使用fortran的方法
    目录一. 编译器的下载1.编译器的选择2. MinGW w64文件下载3.环境变量的配置二. VScode调试相关插件1. 安装VScode2.命令行窗口编辑3.安装插件三.编译运行...
    99+
    2022-11-12
  • SpringCloud Config的使用和配置方法
    这篇文章主要介绍“SpringCloud Config的使用和配置方法”,在日常操作中,相信很多人在SpringCloud Config的使用和配置方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Sprin...
    99+
    2023-06-20
  • vue使用vite配置跨域及环境配置的方法
    这篇文章主要介绍“vue使用vite配置跨域及环境配置的方法”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue使用vite配置跨域及环境配置的方法”文章能帮助大家解决问题。如何配置跨域,代理域名不...
    99+
    2023-07-02
  • Log4j properties的配置及其使用方法
    这篇文章主要讲解了“Log4j properties的配置及其使用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Log4j properties的配置及其使用方法”吧!目录一、log4j...
    99+
    2023-06-20
  • Django使用redis配置缓存的方法
    目录前言django应用redis缓存1.settings配置2.全站缓存3.视图函数缓存4.低级缓存5.session缓存对于非经常更新的服务器数据,若每次都从硬盘读取一次,会浪费...
    99+
    2022-11-12
  • 使用nginx配置访问wgcloud的方法
    nginx配置如下: 如http://172.17.188.27/wgcloud  server { listen 80; ...
    99+
    2022-11-12
  • spring使用JavaConfig进行配置的方法
    JavaConfig,是在 Spring 3.0 开始从一个独立的项目并入到 Spring 中的。JavaConfig 可以看成一个用于完成 Bean 装配的 Spring 配置文件...
    99+
    2022-11-12
  • 微信小游戏开发配置API接口的方法
    这篇文章主要介绍“微信小游戏开发配置API接口的方法”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“微信小游戏开发配置API接口的方法”文章能帮助大家解决问题。微信小游戏开发者通过在根目录编写一个ga...
    99+
    2023-06-26
  • Springboot项目中使用redis的配置方法
    小编给大家分享一下Springboot项目中使用redis的配置方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!springboot是什么springboot一...
    99+
    2023-06-14
  • Golang使用ini库读取配置的方法
    本篇内容介绍了“Golang使用ini库读取配置的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!go-ini的分区go-ini的多个配置...
    99+
    2023-06-30
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作