FreeSql 提供使用 FluentApi, 在外部配置实体的数据库特性,FluentApi 的方法命名与 Attribute 保持一致,如下:

fsql.CodeFirst
.ConfigEntity<TestFluenttb1>(a => {
a.Name("table1");
a.Property(b => b.Id).Name("PkId").IsIdentity(true);
})
.ConfigEntity<TestFluenttb2>(a => {
//...
}); class TestFluenttb1
{
public int Id { get; set; }
public string name { get; set; } = "defaultValue";
}
[Table(Name = "table2")]
class TestFluenttb2
{
public int Id { get; set; }
public string name { get; set; } = "defaultValue";
}

fsql 是一个 IFreeSql 对象

FluentApi 方法命名不优雅,主要考虑到和 Attribute 一致,可以减少理解成本。如果你对 EFCore FluentApi 很熟悉可以使用扩展包:

dotnet add package FreeSql.DbContext

fsql.CodeFirst.Entity<Song>(eb =>
{
eb.ToTable("tb_song");
eb.Ignore(a => a.Field1);
eb.Property(a => a.Title).HasColumnType("varchar(50)").IsRequired();
eb.Property(a => a.Url).HasMaxLength(100); eb.Property(a => a.RowVersion).IsRowVersion();
eb.Property(a => a.CreateTime).HasDefaultValueSql("getdate()"); eb.HasKey(a => a.Id);
eb.HasIndex(a => a.Title).IsUnique().HasName("idx_xxx11"); //一对多、多对一
eb.HasOne(a => a.Type).HasForeignKey(a => a.TypeId).WithMany(a => a.Songs); //多对多
eb.HasMany(a => a.Tags).WithMany(a => a.Songs, typeof(Song_tag));
}); fsql.CodeFirst.Entity<SongType>(eb =>
{
eb.HasMany(a => a.Songs).WithOne(a => a.Type).HasForeignKey(a => a.TypeId); eb.HasData(new[]
{
new SongType
{
Id = 1,
Name = "流行",
Songs = new List<Song>(new[]
{
new Song{ Title = "真的爱你" },
new Song{ Title = "爱你一万年" },
})
},
new SongType
{
Id = 2,
Name = "乡村",
Songs = new List<Song>(new[]
{
new Song{ Title = "乡里乡亲" },
})
},
});
}); public class SongType
{
public int Id { get; set; }
public string Name { get; set; } public List<Song> Songs { get; set; }
} public class Song
{
[Column(IsIdentity = true)]
public int Id { get; set; }
public string Title { get; set; }
public string Url { get; set; }
public DateTime CreateTime { get; set; } public int TypeId { get; set; }
public SongType Type { get; set; }
public List<Tag> Tags { get; set; } public int Field1 { get; set; }
public long RowVersion { get; set; }
}
public class Song_tag
{
public int Song_id { get; set; }
public Song Song { get; set; } public int Tag_id { get; set; }
public Tag Tag { get; set; }
} public class Tag
{
[Column(IsIdentity = true)]
public int Id { get; set; }
public string Name { get; set; } public List<Song> Songs { get; set; }
}

优先级

FluentApi < 实体特性

系列文章导航

FreeSql (四)实体特性 Fluent Api的更多相关文章

  1. FreeSql (三)实体特性

    主键(Primary Key) class Topic { [Column(IsPrimary = true)] public int Id { get; set; } } 约定: 当没有指明主键时, ...

  2. Fluent API 配置

    EF里实体关系配置的方法,有两种: Data Annotation方式配置 也可以 Fluent API 方式配置 Fluent API 配置的方法 EF里的实体关系 Fluent API 配置分为H ...

  3. Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)

    在前面的随笔<Entity Framework 实体框架的形成之旅--Code First的框架设计(5)>里介绍了基于Code First模式的实体框架的经验,这种方式自动处理出来的模式 ...

  4. 一步一步学EF系列1【Fluent API的方式来处理实体与数据表之间的映射关系】

    EF里面的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面,还有一个就是F ...

  5. 一步一步学EF系列二【Fluent API的方式来处理实体与数据表之间的映射关系】

    EF里面的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面,还有一个就是F ...

  6. 8.2 使用Fluent API进行实体映射【Code-First系列】

    现在,我们来学习怎么使用Fluent API来配置实体. 一.配置默认的数据表Schema Student实体 using System; using System.Collections.Gener ...

  7. Entity Framework Code First (四)Fluent API - 配置属性/类型

    上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...

  8. 【译】第23节---Fluent API - 实体映射

    原文:http://www.entityframeworktutorial.net/code-first/configure-entity-mappings-using-fluent-api.aspx ...

  9. 第十八篇 .NET高级技术之Linq与EF Code-First Fluent API基础讲解

    1.FluentApi简介 在这里提供了一个fluentapi基础的DEMO然后咱们在进一步的学习,直接上干货. 第一步在数据库创建一个表:person 第二步:新建控制台程序FluentAPI 第三 ...

随机推荐

  1. Java并发编程实战笔记—— 并发编程1

    1.如何创建并运行java线程 创建一个线程可以继承java的Thread类,或者实现Runnabe接口. public class thread { static class MyThread1 e ...

  2. java面向对象中的集合

    1.学习集合的原因? A.数组是面向过程的,集合是面向对象的. B.集合是类,具备类的封装,继承,多态...超强功能. C.数组是固定长度,集合是可变长度 D.数组没办法获得真实的元素个数:集合可以. ...

  3. linux装OpenOffice后传---中文乱码的解决

    上一篇的博客已经详细的介绍了linux系统上如何安装OpenOffice,安装之后使用发现转换的pdf出现中文乱码.后来发现是linux上没有中文对应的那个字体. 字体准备 在windows上的位置 ...

  4. vue过滤器微信小程序过滤器和百度智能小程序过滤器

    因为最近写了微信小程序和百度小程序,用到了过滤器,感觉还挺好用的,所以就来总结一下,希望能帮到你们. 1. 微信小程序过滤器: 1.1:首先建一个单独的wxs后缀的文件,一般放在utils文件夹里面. ...

  5. c# NPOI 导出23万条记录耗时12秒

    先上测试代码: string connectionString = "Server=localhost;Initial Catalog=******;User ID=sa;Password= ...

  6. SpringBoot学习------SpringBoot使用Thymleaf模块访问不了静态页面

    SpringBoot使用Thymleaf模块访问不了静态页面 最近学习SpringBoot的过程中使用了Thymeleaf模块引擎,页面发送请求后老是无法显示静态页面,所有的步骤都是参考资料来执行,自 ...

  7. Jmeter接口自动化实例(使用Beanshell保存csv文件、csv参数化、setUp线程组)

    很久没更新博客了,荒废了很久了,今天更新一下博客,主要记录一下子最近遇到的问题和解决方法:blonde_woman: 这篇文章主要记录的是jmeter批量跑接口中遇到的各种疑难,主要涉及到的问题如下 ...

  8. Python面试总结篇

    Python Coding Interview Questions and Answers 面试题一:逻辑运算赋值 v1 = 1 or 9 v2 = 0 or 9 # print(v1, v2)会输出 ...

  9. 并查集(不相交集合)详解与java实现

    目录 认识并查集 并查集解析 基本思想 如何查看a,b是否在一个集合? a,b合并,究竟是a的祖先合并在b的祖先上,还是b的祖先合并在a上? 其他路径压缩? 代码实现 结语 @(文章目录) 认识并查集 ...

  10. 用故事解析setTimeout和setInterval(内含js单线程和任务队列)

    区别: setTimeout(fn,t): 延迟调用,超过了时间就调用回调函数,返回一个id,使用clearTimeout(id)取消执行. 注意:取消了里面的回调函数就不执行了哦,而不是取消的时候就 ...