preface

Tried adO.net, Dapper, EF, and Repository, and even wrote my own generator tools for regular CRUD operations.

Inconveniences of their daily operation:

  • Need to declare before each use, and then operate;

  • Many people have an entity class that corresponds to an operation class (or DAL, DbContext, Repository);

BaseEntity is a very simple CodeFirst development method, especially for single table or multi-table CRUD, using inheritance to save the repeated attributes (creation time, ID, etc.) of each entity class, software deletion and other functions, CRUD operations do not always consider the use of warehousing;

This article introduces BaseEntity as a minimalist CRUD operation.

Functional features

  • Automatic migration of Entity Structure (CodeFirst) to database;

  • A method of directly manipulating entities for CRUD operations;

  • Simplify user-defined entity types by eliminating primary key and common field configurations (such as CreateTime and UpdateTime).

  • Realize soft delete logic of single table and multi table query;

The statement

Demonstration project: github.com/2881099/Fre…

Refer to the baseentity. cs source code (about 100 lines), copy it for use in the project, and then add the nuget reference package:

dotnet add package FreeSql.Repository

dotnet add package FreeSql.Provider.Sqlite

1, define a primary key int and increment the entity type. If the BaseEntity TKey is set to int/long, the primary key is increment.

public class UserGroup : BaseEntity<UserGroup, int> { public string GroupName { get; set; }}Copy the code

If you don’t want the primary key to increment, you can override the property:

public class UserGroup : BaseEntity<UserGroup, int> { [Column(IsIdentity = false)] public override int Id { get; set; } public string GroupName { get; set; }}Copy the code

For more feature configurations for entities, see: github.com/2881099/Fre…

2, define a primary key Guid entity type, save data automatically generated sequential non-repeating Guid value (do not specify their own guid.newGUID ());

public class User : BaseEntity<UserGroup, Guid> { public string UserName { get; set; }}Copy the code

Define entity types with multiple primary keys. You can override the field name in the static constructor.

public class User2 : BaseEntity<User2, Guid, int> { static User2() { User2.Orm.CodeFirst.ConfigEntity<User2>(t => { t.Property(a => a.PkId1).Name("UserId"); t.Property(a => a.PkId2).Name("Index"); }); } public string Username { get; set; }}Copy the code

Use the CRUD

/ / add
var item = new UserGroup { GroupName = "Group one" };
item.Insert();

/ / update
item.GroupName = "Group 2";
item.Update();

// Add or update
item.Save();

/ / soft delete
item.Delete();

// Restore soft delete
item.Restore();

// Get the object based on the primary key
var item = UserGroup.Find(1);

// Query data
var items = UserGroup.Where(a => a.Id > 10).ToList();
Copy the code

Select is a query object, using the same method as freesqL.isELECT;

When multiple table queries are supported, soft delete conditions are attached to each table.

For more information, please refer to github.com/2881099/Fre…

The resources

Beginner’s Guide | “Select” | “Update” | “Insert” | “Delete”
Expression function | “CodeFirst” | “DbFirst” | “The BaseEntity”
“Repository” | “The UnitOfWork” | The Filter | Optimism Lock | “The DbContext”
Unread | Partition table | “The tenants” | The AOP | Black Tech | Update log