[open source]. NETCORE. Netframework xamarin uses ORM FreeSql to access Sqlite

Keywords: Programming SQLite Database Navicat MySQL

1. Create project

We use the console type project to test the insertion, deletion, update, query and other functions, create a console project, and use 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 ORM

static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.Sqlite, "data source=test.db")
    .UseMonitorCommand(cmd => Trace.WriteLine($"Threading:{cmd.CommandText}\r\n"))
    .UseAutoSyncStructure(true) //Automatically create and migrate entity table structure
    .UseNoneCommandParameter(true)
    .Build();

4. Insert data

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

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

var users = new []
{
    new User { UserName = "dm2", PassWord = "1234" },
    new User { UserName = "dm3", PassWord = "12345" },
    new User { UserName = "dm4", PassWord = "123456" }
};
repo.Insert(users);
//Batch insertion

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(); //Query a piece of data

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

7. Delete data

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

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

epilogue

This article briefly introduces how to use FreeSql to access Sqlite database in. net core 3.1 environment. At present, FreeSql also supports. net framework 4.0 and xamarin platform.

In addition to adding, deleting, checking and modifying, FreeSql also supports many functions, so it can't be demonstrated one by one and can't be covered in one article.

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

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

Thank you for your support!

Posted by ubaldc36 on Sat, 18 Apr 2020 09:02:50 -0700