使用SqlCompactDatabase寻找Dapper.Rainbow的示例

时间:2012-05-28 18:47:02

标签: sql-server-ce dapper dapper-rainbow

我正在尝试使用Sam Saffron发布的Dapper Rainbow Sample(https://gist.github.com/1599013)和Sql Server Ce 4数据库。下面是我的代码示例,我遇到的问题是db.Products为null。看起来像Init()不起作用。 (数据库确实有必要的表)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;

using Dapper;
using Dapper.Rainbow;

// to have a play, install Dapper.Rainbow from nuget

namespace TestDapper
{
    class Program
    {
        // no decorations, base class, attributes, etc 
        class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public DateTime? LastPurchase { get; set; }
        }

        // container with all the tables 
        class MyDatabase : SqlCompactDatabase<MyDatabase>
        {
            public Table<Product> Products { get; set; }
        }

        static void Main(string[] args)
        {
            var cnn = new SqlCeConnection("Data Source=test.sdf");
            cnn.Open();

            var db = MyDatabase.Init(cnn);

            if (db.Products == null)
            {
                Console.WriteLine("db.Products is null");
                Console.ReadKey();
                return;
            }

            int? productId = db.Products.Insert(new { Name = "Hello", Description = "Nothing" });
            var product = db.Products.Get((int)productId);

            Console.WriteLine("id: {0} name: {1} desc: {2} last {3}", product.Id, product.Name, product.Description, product.LastPurchase);
            // id: 1 name: Hello World desc: Nothing last 12/01/2012 5:49:34 AM

            Console.WriteLine("deleted: {0}", db.Products.Delete(product.Id));
            // deleted: True 


            Console.ReadKey();
        }
    }
}

0 个答案:

没有答案
相关问题