PetaPoco初体验(转)

大部分转自: http://landyer.com/archives/138

PetaPoco C#微型ORM框架,基本无需配置,仅由单个cs文件构成,支持.net3.5 .net4.0。

截稿时PetaPoco的官方最新版本为5.0.1。我不采用5.0.1版本,因为我用的是.net3.5,而5.0.1的版本中用到了一个类System.Tuple,这个类是从.net4.0才开始有的。(当然也可以自己实现这个类,不过像我这样的初学者还是算了吧)。

NuGet图形界面中默认列出的只有最新版本,一装就是5.0.1, 那就是使用NuGet命令行。

1.依次打开《工具》《库程序包管理器》《程序包管理器控制台》

2.敲入命令对可用的版本进行查询get-package –listavailable –allversion –filter petapoco,如图

3.敲入命令进行安装install-package petapoco –version 4.0.3

配置PetaPoco

现在又遇到一个问题,使用Ctrl+Shift+B编译项目的时候报错,说dynamic神马的不存在。

这是因为Dynamic也是.net4.0的。PetaPoco官方提供了一个编译时的选项来开启&关闭对dynamic的支持。

下面来进行编译选项的设置:

  1. 在项目上《右键》《属性》
  2. 选择左侧tab的第二项《生成》,在《条件编译符号》中输入PETAPOCO_NO_DYNAMIC
  3. 保存,再次Ctrl+Shift+B,进行编译。这次就OK了。

配置PetaPoco T4模板

  1. 打开Database.tt文件

    ConnectionStringName = "WorkDemo";            // Uses last connection string in config if not specified
    Namespace = "WorkDemo.Tests.Models";
    RepoName = "PPUtils";            //辅助类的名字

PetaPoco使用示例

    一对多 和 多对一

  • 为Account增加一个额外的属性AccountUsers

  • 为Account_User增加一个额外的属性Account
  • 将Account和Account_User进行关联
    public partial class Account
    {
        [PetaPoco.Ignore]
        public List<Account_User> AccountUsers { get; set; } 
    }
    public partial class Account_User
    {
        [PetaPoco.Ignore]
        public Account Account { get; set; }
    }

    //
    public class UserAccountRelator
    {
        private Dictionary<long, Account> accounts = new Dictionary<long, Account>();

        public Account_User MapIt(Account account, Account_User user)
        {
            Account accountExisting;
            if (accounts.TryGetValue(account.Id, out accountExisting))
            {
                account = accountExisting;
            }
            else
                accounts.Add(account.Id, account);

            user.Account = account;
            return user;
        }
    }

    //
    public class AccountUserRelator
    {
        public Account current;

        public Account MapIt(Account account, Account_User user)
        {
            if (account == null)
                return current;

            if (current != null && current.Id == account.Id)
            {
                current.AccountUsers.Add(user);
                return null;
            }

            var prev = current;

            current = account;
            current.AccountUsers = new List<Account_User>();
            current.AccountUsers.Add(user);

            return prev;
        }
    }

    查询:

            var users = db.Fetch<Account, Account_User, Account_User>(
                new UserAccountRelator().MapIt,
                "select * from Account A left join Account_User AU on AU.AccountId=A.Id "
                );
            foreach (Account_User user in users)
            {
                Console.WriteLine("{0} - {1}", user.Id, user.UserName);
            }

            var accounts = db.Fetch<Account, Account_User, Account>(
                new AccountUserRelator().MapIt,
                "select * from Account A left join Account_User AU on AU.AccountId=A.Id "
                );
            foreach (Account acc in accounts)
            {
                Console.WriteLine("{0} - {1}", acc.Id, acc.Name);
            }
原文地址:https://www.cnblogs.com/dfg727/p/3878117.html