主键(Primary Key)
class Topic { [Column(IsPrimary = true)] public int Id { get; set; } }
约定:

*
当没有指明主键时,命名为 id 的字段将成为主键;(不区分大小写)

*
当主键是 Guid 类型时,插入时会自动创建(有序、不重复)的值,所以不需要自己赋值;(支持分布式)

自增(Identity)
class Topic { [Column(IsIdentity = true)] public int Id { get; set; } }
约定:

* 当没有指明主键时,标记自增的成员将成为主键;
唯一键(Unique Key)
class AddUniquesInfo { public Guid id { get; set; } [Column(Unique =
"uk_phone")] public string phone { get; set; } [Column(Unique =
"uk_group_index, uk_group_index22")] public string group { get; set; }
[Column(Unique = "uk_group_index")] public int index { get; set; }
[Column(Unique = "uk_group_index22")] public string index22 { get; set; } }
唯一键,在多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。

数据库类型(DbType)
class Topic { [Column(DbType = "varchar(128) NOT NULL")] public string Title {
get; set; } }
可以在类型上指定 NOT NULL,也可以通过 [Column(IsNullable = false)] 设置;

0.9.12 版本增加了对 MaxLength 特性的解析,避免字符串常用时的麻烦,上面的 varchar(128) 可改写成:
class Topic { [MaxLength(128)] public string Title { get; set; } }
说明:由于内部按名称反射查找特性的,所以 MaxLengthAttribute 可以在任意地方定义。 该特性通常定义在
System.ComponentModel.DataAnnotations.MaxLengthAttribute。 如果找不到该类,可自行在项目中定义名称为
MaxLengthAttribute 的特性类,如下: public class MaxLengthAttribute : Attribute {
public int Length { get; } public MaxLengthAttribute(int length) { this.Length
= length; } }
可空(Nullable)
class Topic { [Column(IsNullable = false)] public string Title { get; set; } }
在不指定 DbType、IsNullable 时,FreeSql 提供默认设定,如:

* int -> not null(不可为空)
* int? -> null(可空)
一般在使用 string 类型时,才需要手工指明是否可空(string 默认可空);

忽略(Ignore)
class Topic { [Column(IsIgnore = true)] public string Title { get; set; } }
当实体有属性不需要映射的时候使用,内部自动忽略了对象的映射;

当实体内的属性不是可接受的类型时,可以不用指定该特定,如下不必要的指定:
class Topic { [Column(IsIgnore = true)] public Topic Parent { get; set; } }
乐观锁(RowVersion)
class Topic { public Guid id { get; set; } public string Title { get; set; }
[Column(IsVersion = true)] public int Version { get; set; } }
更新整个实体数据时,在并发情况下极容易造成旧数据将新的记录更新。

行级锁的原理,是利用实体某字段,如:long version,更新前先查询数据,此时 version 为 1,更新时产生的 SQL 会附加 where
version = 1,当修改失败时(即 Affrows == 0)抛出异常。

每个实体只支持一个行级锁属性。

适用 SetSource 更新,无论使用什么方法更新 version 的值都会增加 1

自定义类型映射(MapType)
class EnumTestMap { public Guid id { get; set; } [Column(MapType =
typeof(string))] public ToStringMapEnum enum_to_string { get; set; }
[Column(MapType = typeof(string))] public ToStringMapEnum?
enumnullable_to_string { get; set; } [Column(MapType = typeof(int))] public
ToStringMapEnum enum_to_int { get; set; } [Column(MapType = typeof(int?))]
public ToStringMapEnum? enumnullable_to_int { get; set; } [Column(MapType =
typeof(string))] public BigInteger biginteger_to_string { get; set; }
[Column(MapType = typeof(string))] public BigInteger?
bigintegernullable_to_string { get; set; } } public enum ToStringMapEnum { 中国人,
abc, 香港 }
应该不需要解释了吧?

BigInteger 都可以映射使用了,但请注意:仅仅是 CURD 方便, Equals == 判断可以使用,无法实现 + - * / 等操作;

v0.9.15 版本还可以将值对象映射成 typeof(string),安装扩展包:

dotnet add package FreeSql.Extensions.JsonMap
fsql.UseJsonMap(); //开启功能 class TestConfig { public int clicks { get; set; }
public string title { get; set; } } [Table(Name = "sysconfig")] public class
S_SysConfig<T> { [Column(IsPrimary = true)] public string Name { get; set; }
[JsonMap] public T Config { get; set; } }
字段位置(Position)

适用场景:当实体类继承时,CodeFirst创建表的字段顺序可能不是想要的,通过该特性可以设置顺序。

创建表时指定字段位置,如:[Column(Position = 1],可为负数即反方向位置;

名称

FreeSql 默认使用实体的类名,或属性名与数据库映射,也可以指定映射的名称;

指定实体的表名,指定 Name
后,实体类名变化不影响数据库对应的表。FreeSql尽量支持了对多数据库或schema支持,不防试试指定表名为:其他数据库.表名,不同数据库的指定方式有差异,这一点以后深入解答。
[Table(Name = "db2.tb_topic111")] class Topic { //... }

指定实体的表名,修改为实体类名。指定数据库旧的表名,修改实体命名时,同时设置此参数为修改之前的值,CodeFirst才可以正确修改数据库表;否则将视为【创建新表】。
[Table(OldName = "Topic")] class Topic2 { //... }
实体的属性也有相同的功能,[Column(Name = "xxx")]

禁用迁移

IFreeSql.CodeFirst.IsAutoSyncStructure 可设置全局【自动迁移结构】功能,也可通过
FreeSqlBuilder.UseAutoSyncStructure(true) 创建 IFreeSql 的时候设置功能。

当【实体类】对应的是数据库【视图】或者其他时,可通过 [Table(DisableSyncStructure = true)] 禁用指定的实体迁移操作。
[Table(DisableSyncStructure = true)] class ModelDisableSyncStructure {
[Column(IsPrimary = false)] public int pkid { get; set; } }
备注

FreeSql CodeFirst 支持将 c# 代码内的注释,迁移至数据库的备注。先决条件:

1、实体类所在程序集,需要开启 xml 文档功能;

2、xml 文件必须与程序集同目录,且文件名:xxx.dll -> xxx.xml;

系列文章导航

*
(一)入门 <https://www.cnblogs.com/FreeSql/p/11531300.html>

*
(二)自动迁移实体 <https://www.cnblogs.com/FreeSql/p/11531301.html>

*
(三)实体特性

*
(四)实体特性 Fluent Api <https://www.cnblogs.com/FreeSql/p/11531304.html>

*
(五)插入数据 <https://www.cnblogs.com/FreeSql/p/11531306.html>

*
(六)批量插入数据 <https://www.cnblogs.com/FreeSql/p/11531309.html>

*
(七)插入数据时忽略列 <https://www.cnblogs.com/FreeSql/p/11531316.html>

*
(八)插入数据时指定列 <https://www.cnblogs.com/FreeSql/p/11531318.html>

*
(九)删除数据 <https://www.cnblogs.com/FreeSql/p/11531320.html>

*
(十)更新数据 <https://www.cnblogs.com/FreeSql/p/11531321.html>

*
(十一)更新数据 Where <https://www.cnblogs.com/FreeSql/p/11531324.html>

*
(十二)更新数据时指定列 <https://www.cnblogs.com/FreeSql/p/11531327.html>

*
(十三)更新数据时忽略列 <https://www.cnblogs.com/FreeSql/p/11531334.html>

*
(十四)批量更新数据 <https://www.cnblogs.com/FreeSql/p/11531335.html>

*
(十五)查询数据 <https://www.cnblogs.com/FreeSql/p/11531339.html>

*
(十六)分页查询 <https://www.cnblogs.com/FreeSql/p/11531341.html>

*
(十七)联表查询 <https://www.cnblogs.com/FreeSql/p/11531346.html>

*
(十八)导航属性 <https://www.cnblogs.com/FreeSql/p/11531352.html>

*
(十九)多表查询 <https://www.cnblogs.com/FreeSql/p/11531362.html>

*
(二十)多表查询 WhereCascade <https://www.cnblogs.com/FreeSql/p/11531372.html>

*
(二十一)查询返回数据 <https://www.cnblogs.com/FreeSql/p/11531376.html>

*
(二十二)Dto 映射查询 <https://www.cnblogs.com/FreeSql/p/11531381.html>

*
(二十三)分组、聚合 <https://www.cnblogs.com/FreeSql/p/11531384.html>

*
(二十四)Linq To Sql 语法使用介绍 <https://www.cnblogs.com/FreeSql/p/11531392.html>

*
(二十五)延时加载 <https://www.cnblogs.com/FreeSql/p/11531395.html>

*
(二十六)贪婪加载 Include、IncludeMany、Dto、ToList
<https://www.cnblogs.com/FreeSql/p/11531404.html>

*
(二十七)将已写好的 SQL 语句,与实体类映射进行二次查询
<https://www.cnblogs.com/FreeSql/p/11531416.html>

*
(二十八)事务 <https://www.cnblogs.com/FreeSql/p/11531423.html>

*
(二十九)Lambda 表达式 <https://www.cnblogs.com/FreeSql/p/11531425.html>

*
(三十)读写分离 <https://www.cnblogs.com/FreeSql/p/11531430.html>

*
(三十一)分区分表 <https://www.cnblogs.com/FreeSql/p/11531435.html>

*
(三十二)Aop <https://www.cnblogs.com/FreeSql/p/11531471.html>

*
(三十三)CodeFirst 类型映射 <https://www.cnblogs.com/FreeSql/p/11531543.html>

*
(三十四)CodeFirst 迁移说明 <https://www.cnblogs.com/FreeSql/p/11531550.html>

*
(三十五)CodeFirst 自定义特性 <https://www.cnblogs.com/FreeSql/p/11531576.html>

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信