FreeSql (c) the entity properties

Primary key (Primary Key)

class Topic {
    [Column(IsPrimary = true)]
    public int Id { get; set; }
}

Conventions:

  • When not specified primary key named id field becomes the primary key; (case insensitive)

  • When the primary key is the Guid type, is automatically created when you insert (ordered, not duplicate) value, so do not need to own assignment; (support distributed)

Increment (Identity)

class Topic {
    [Column(IsIdentity = true)]
    public int Id { get; set; }
}

Conventions:

  • When the primary key is not specified, the self-energizing member mark becomes the primary key;

Unique Key (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; }
}

Unique key, specify a plurality of attributes in the same identifier, representative of the United bond; a plurality UniqueKey names separated by commas.

Database type (DbType)

class Topic {
    [Column(DbType = "varchar(128) NOT NULL")]
    public string Title { get; set; }
}

NOT NULL can be specified in type, can also [Column (IsNullable = false)] is provided;

0.9.12 version adds parsing MaxLength properties, avoiding the trouble of frequently used character string, the above varchar (128) can be rewritten as:

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;
    }
}

Can be empty (Nullable)

class Topic {
    [Column(IsNullable = false)]
    public string Title { get; set; }
}

Do not specify DbType, IsNullable time, FreeSql provided in the default setting, such as:

  • int -> not null (not empty)
  • int -?> null (can be empty)

String type generally in use, only need to be specified whether to manually empty (null string may default);

Ignore (Ignore)

class Topic {
    [Column(IsIgnore = true)]
    public string Title { get; set; }
}

When the entity has attributes need not be used when mapping, ignoring internal automatic mapping object;

When the entity is not within the acceptable attribute type, that can not specify a particular, specified as unnecessary:

class Topic {
    [Column(IsIgnore = true)]
    public Topic Parent { get; set; }
}

Optimistic locking (RowVersion)

class Topic {
    public Guid id { get; set; }
    public string Title { get; set; }

    [Column(IsVersion = true)]
    public int Version { get; set; }
}

When updating the entire entity data, in the concurrent case very easily lead to a new record old data updates.

Row level locking principle, is to use a field entity, such as: long version, first check data before updated, version of case 1, SQL generated when additional updates where version = 1, when the modification failure (i.e. Affrows == 0) throw an exception.

Each entity supports only one row-level locking property.

Applicable SetSource updated value no matter what method will increase the updated version of 1

Custom type mapping (the 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, 香港 }

You should not need to explain it?

BigInteger can be mapped using, but please note: only convenient CURD, Equals == judge can use, can not be achieved + - * / and other operations;

v0.9.15 release values ​​may also be mapped to the object typeof (string), extended package installation:

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; }
}

Field position (Position)

Applicable scene: When an entity class inheritance, CodeFirst create a table of field order may not want, you can set the order by this feature.

Create a table position specified field, such as: [Column (Position = 1], i.e., the reverse direction may be a negative position;

name

FreeSql default class name of the entity or attribute name with a database mapping, mapping of the name can also be specified;

Table designated entity, after a specified Name, the entity class name changes do not affect the corresponding database tables. FreeSql try to support or support for multi-database schema, not anti-try to specify the table name: Other database table names are different ways to specify a different database, which is after in-depth answers.

[Table(Name = "db2.tb_topic111")]
class Topic {
  //...
}

Specifying the name of the entity, modified entity class name. Specifies the old database table names, modify an entity name, and set this parameter to the value before modification, CodeFirst can modify database tables correctly; otherwise will be deemed to create a new table [].

[Table(OldName = "Topic")]
class Topic2 {
  //...
}

Entity attributes have the same function, [Column (Name = "xxx")]

Disable Migration

IFreeSql.CodeFirst.IsAutoSyncStructure can be set to automatically migrate structure [global] function, but also through time FreeSqlBuilder.UseAutoSyncStructure (true) to create IFreeSql setup functions.

When [] corresponds to the entity classes View or other database, the migration operation by the entity [Table (DisableSyncStructure = true)] to disable specified.

[Table(DisableSyncStructure = true)]
class ModelDisableSyncStructure {
    [Column(IsPrimary = false)]
    public int pkid { get; set; }
}

Remark

FreeSql CodeFirst supports comments in the c # code to migrate to Notes databases. prerequisites:

1, where the entity class assembly, need to open the xml document functions;

2, xml files must be set the same directory with the program, and the file name: xxx.dll -> xxx.xml;

Guess you like

Origin www.cnblogs.com/FreeSql/p/11531302.html