当前位置:网站首页>[open source]. Net uses ORM to access Huawei gaussdb database

[open source]. Net uses ORM to access Huawei gaussdb database

2020-11-08 15:49:00 FreeSql

Preface

Huawei GaussDB It's an enterprise AI-Native Distributed database .GaussDB use MPP(Massive Parallel Processing) framework , Support row storage and column storage , Provide PB(Petabyte,2 Of 50 The power byte ) Level of data processing capacity . It can provide a general computing platform with high cost performance for super large scale data management , It can also be used to support various data warehouse systems 、BI(Business Intelligence) Systems and decision support systems , Provide service for decision analysis of upper application .

As huawei 、 ZTE Affairs , The domestic database market is believed to be the trend in the future , throughout .net core The whole circle of China's Huawei GaussDB Database support is almost 0, Today we use FreeSql ORM To experience domestic Huawei GaussDB database ( Although it is to take pgsql Source code modified ).

Overall, , Huawei GaussDB and pgsql Grammar compatible , But at present, it can only be done through ODBC visit ( No, ado.net drive , Maybe npgsql You can also visit , If you have a partner, please at We ),FreeSql Provide ODBC Multiple database access implementation .

Because I have no test environment , But group friends have been able to access successfully using the methods in the article . The image references in this article are screenshots of other databases , The general situation is the same .

1、 Installation environment

database server : Huawei GaussDB

Download address :https://e.huawei.com/cn/products/cloud-computing-dc/gaussdb

.NET edition :.net core 3.1

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

Developing machines :windows 10

ODBC drive : Please follow Huawei GaussDB Document installation

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.Odbc

dotnet add package FreeSql.Repository

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

4、 initialization ORM

static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.OdbcPostgreSQL, 
    "odbc  Connection string ")
    .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);

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 

6、 Update data

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

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 .net core 3.1 Use in the environment FreeSql For domestic Huawei GaussDB Database access , at present FreeSql And support .net framework 4.0 and xamarin On the platform .

Domestic database is the development trend in the future , There is no limit to the autonomy of others , I saw some people in several groups saying that the company is preparing to fully use the domestic system + Domestic database .

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/ Reach a dream , 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]所创,转载请带上原文链接,感谢