当前位置:网站首页>[open source] Net ORM accessing Firebird database

[open source] Net ORM accessing Firebird database

2022-07-07 21:49:00 FreeSql

Preface

Firebird Is a cross platform relational database system , At present, it can run in Windows、linux And all kinds of Unix On the operating system , Provides most of SQL-99 Standard functions . It can run as a database server in a multi-user environment , It also provides the implementation of embedded database .

Firebird Born out of Borland The company's open source database Interbase6.0, It is a completely non-commercial product , use C and C++ Development . Because of and interbase By blood , Most of the interbase Our development tools can be directly applied to Firebird In development .Firebird Use Mozilla Public License v.1.1 License issue .

FreeSql Support Firebird embedded database , And Sqlite It also belongs to the local database , And the reliability is stronger than sqlite, Database file however 10 mega size .

1、 Installation environment

Database environment :Firebird ( Embedded version )

Download address :https://firebirdsql.org/en/server-packages/

.NET edition :.net6.0

Download address :https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/install

Developing machines :windows 10

2、 Create project

We use console Type project test Insert 、 Delete 、 to update 、 Inquire about And so on , Create a console project , Use command :

dotnet new console

dotnet add package FreeSql.Provider.Firebird

dotnet add package FreeSql.Repository

3、 Create a solid model

using System;
using FreeSql.DataAnnotations;

[Table(Name = "USER_FIREBIRD")]
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; }
}

4、 initialization ORM

static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.Firebird, 
    @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=3")
    .UseMonitorCommand(cmd => Trace.WriteLine($" Threads :{cmd.CommandText}\r\n"))
    .UseAutoSyncStructure(true) // Automatically create 、 Migrate entity table structure 
    .UseNameConvert(NameConvertType.ToUpper)
    .Build();

5、 insert data

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

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

First access to automatically create tables

var users = new []
{
    new User { UserName = "gaussdb2", PassWord = "1234" },
    new User { UserName = "gaussdb3", PassWord = "12345" },
    new User { UserName = "gaussdb4", PassWord = "123456" }
};
repo.Insert(users);
// Batch insert 

Due to primary key ID It's self increasing , This batch insertion will be split into multiple executions , And backfill the value users[0..2].Id

6、 Update data

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

Only update changed attributes ( Field )

7、 Query data

var one = fsql.Select<User>(1).First(); // Query a piece of data 

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

8、 Delete data

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

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

Conclusion

This article gives a brief introduction to .net6.0 Use in the environment FreeSql Yes Firebird Database access , at present FreeSql And support .net framework 4.0 and xamarin On the platform .

except Add or delete check change ,FreeSql It also supports many functions , Not one by one , There is no end to an article .

FreeSql yes .NETCore/.NetFramework/Xamarin Under the platform ORM Open source project , Support SqlServer/MySql/PostgreSQL/Oracle/Sqlite/Firebird/Clickhouse/ Reach a dream / Supernatural power / Jincang / Hangao , And Huawei GaussDB database , In the future, more domestic databases will be connected to support .

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

Thank you for your support !

原网站

版权声明
本文为[FreeSql]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071931567583.html