[Open source] .NetCore uses ORM FreeSql to access Sqlite

1. Create a project

We test console, project, insert, delete, update, query and other functions to create a console project, using the command:

dotnet new console

dotnet add package FreeSql.Provider.Sqlite

dotnet add package FreeSql.Repository

2. Create a solid model

using System;
using FreeSql.DataAnnotations;

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

    public string UserName { get; set; }
    public string PassWord { get; set; }
    public DateTime CreateTime { get; set; }
}

3. Initialize the ORM

static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.Sqlite, "data source=test.db")
    .UseMonitorCommand(cmd => Trace.WriteLine($"线程:{cmd.CommandText}\r\n"))
    .UseAutoSyncStructure(true) //自动创建、迁移实体表结构
    .UseNoneCommandParameter(true)
    .Build();

4. Insert data

var repo = fsql.GetRepository<User>();

var user = new User { UserName = "sqlite1", PassWord = "123" };
repo.Insert(user);

var users = new []
{
    new User { UserName = "sqlite2", PassWord = "1234" },
    new User { UserName = "sqlite3", PassWord = "12345" },
    new User { UserName = "sqlite4", PassWord = "123456" }
};
repo.Insert(users);
//批量插入

Open the navicat tool and drag the test.db file into:

5. Update data

user.PassWord = "123123";
repo.Update(user);

6. Query data

var one = fsql.Select<User>(1).First(); //查询一条数据

var list = fsql.Select<User>().Where(a => a.UserName.StartsWith("sqlite")).ToList();

7. Delete data

fsql.Delete<User>(1).ExecuteAffrows();

fsql.Delete<User>().Where(a => a.UserName.StartsWith("sqlite")).ExecuteAffrows();

Conclusion

This article briefly introduces the use of FreeSql to access the Sqlite database in the .net core 3.1 environment. Currently FreeSql also supports .net framework 4.0 and xamarin platform.

In addition to adding, deleting, checking, and modifying, FreeSql also supports many functions, so we will not demonstrate them one by one.

FreeSql is an ORM open source project under the .NETCore / .NetFramework / Xamarin platform. It supports SqlServer / MySql / PostgreSQL / Oracle / Sqlite / MsAccess, as well as Dameng database. In the future, more domestic databases will be supported.

Source address: https://github.com/2881099/FreeSql

Thank you for your support!

Guess you like

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