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 < 实体特性

系列文章导航

最新文章

  1. C# winform解决解决窗体第一次设置为最大化后,点击最大化按钮窗体无法居中问题
  2. (转)分布式缓存GemFire架构介绍
  3. ASP.NET Web – AJAX 回送
  4. mysql 的数据文件
  5. SQL SERVER 实现分组合并实现列数据拼接
  6. HDU 5933 ArcSoft&#39;s Office Rearrangement 【模拟】(2016年中国大学生程序设计竞赛(杭州))
  7. poj2409 Let it Bead
  8. Code 16K 码
  9. 高性能IO模型浅析(彩图解释)good
  10. SZU:B54 Dual Palindromes
  11. 为什么重写 equals 方法 必须重写 hashCode
  12. 如何用比较快速的方法掌握Spring的核心——依赖注入,Java web轻量级开发面试教程 读书笔记
  13. codeforces 887B Cubes for Masha 两种暴力
  14. 构建自己的 PHP 框架
  15. Web.config中customErrors异常信息配置
  16. ansible的模块使用说明
  17. [转帖]震惊,用了这么多年的 CPU 利用率,其实是错的
  18. requests SSLError: hostname &#39;ccc.xxx.com&#39; doesn&#39;t match &#39;*.b0.upaiyun.com&#39;
  19. Django--自定义 Command 命令
  20. 团队工作效率分析工具gitstats

热门文章

  1. 最简单的DWR例子
  2. Spring Boot + Security + JWT 实现Token验证+多Provider——登录系统
  3. 记几个 DOM 操作技巧
  4. 常见ASP脚本攻击及防范技巧
  5. 如何用Python实现敏感词的过滤
  6. 阿里云部署 Flask + WSGI + Nginx 转载详解
  7. Oracle 12c Adoption Discussion — Summary
  8. Redis学习总结(一)--Redis入门
  9. MySQL之修改默认引擎和字符集
  10. HDU 2147