c# - 如何在 C# 中实现 ORM

标签 c# mysql .net orm

我正在尝试用 C# 为 MySQL 实现我自己的 ORM,因为我发现它无法完全满足我的要求。

我目前只实现了几个方法(例如保存方法):

public void Save()
    {
        string queryVariables = "";
        foreach (PropertyInfo propertyInfo in this.GetType().GetProperties().Where(x => x.Name != "BdClass" && x.GetValue(this) != null))
        {
            string isLast = this.GetType().GetProperties().Last() == propertyInfo ? "" : ", ";
            queryVariables += $"{propertyInfo.Name} = '{MySqlHelper.EscapeString(propertyInfo.GetValue(this).ToString())}'{isLast}";
        }
        ConnectionSingleton.Query($"INSERT INTO {this.GetType().GetProperties().Single(x => x.Name == "BdClass").GetValue(this)} SET {queryVariables} ON DUPLICATE KEY UPDATE {queryVariables};");
    }

我正在尝试实现 FindAll() 方法,我希望它返回一个列表,其中包含调用该函数的对象的类型。假设我有一个名为 Person 的类,我想从数据库中获取 Person 表的所有行,我想这样写:

List<Person> persons = Person.FindAll();

我正在努力在 FindAll() 方法中创建对象。

最佳答案

看看Insight Db 。我们用它来处理 Sql Server、Oracle 和 DB2。它还支持MySQL。我发现它非常易于使用并且节省了大量时间。

关于c# - 如何在 C# 中实现 ORM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33442757/

相关文章:

c# - 我无法在 ASP.NET 后面的代码中读取按下按钮的 css ID

c# - 在构造函数中访问数组

php - 在 codeigniter 中编写查询的格式

.net - 将 Subversion 与 .net 项目文件一起使用的任何提示?

c# - 防止点击 toast 通知时再次打开应用程序

c# - 使用数据库表中的DataTable更新?

mysql - 存储长 float MySQL

PHP mySQL preg_replace 字符串并存储结果

.net - Azure WebJob QueueTrigger 消息未从队列中删除

c# - 有没有办法从 DataContract 导出 XSD 架构