日期:2014-05-18  浏览次数:20933 次

估计这问题你们也不会,关于EF 4.1 Code First ; 使用 SqlLite 或者 MySQL 等非SQLServer的数据库
以前那些 Database First , Modal First 都不合口味,可是这次 Code First 出现后,就觉得太棒了!

就是我一直梦想的动态实现 类<--->数据库 互相转换。
在一些大运算量的地方,类---->内存数据库, 计算方便又快速。

但是,似乎搞了半天也没能学会用 第三方数据库

C# code
    class Program
    {
        static void Main(string[] args)
        {
            NerdDinners nerdDinners = new NerdDinners();


            var dinner = new Dinner
            {
                Title = "Par at Scott's House",
                EventDate = DateTime.Parse("12/31/2013"),
                Address = "Building 40",
                HostedBy = "scottgu@microsoft.com"
            };
            nerdDinners.Dinners.Add(dinner);
            nerdDinners.SaveChanges();

            var dinners = from d in nerdDinners.Dinners
                          select d;

            foreach (var item in dinners)
            {
                Console.WriteLine(item.ToString());
            }
        }
    }
    public class Dinner
    {
        public int DinnerID { get; set; }
        public string Title { get; set; }
        public DateTime EventDate { get; set; }
        public string Address { get; set; }
        public string HostedBy { get; set; }

        public virtual ICollection<RSVP> RSVPs { get; set; }
    }

    public class RSVP
    {
        public int RsvpID { get; set; }
        public int DinnerID { get; set; }
        public string AttendeeEmail { get; set; }

        public virtual Dinner Dinner { get; set; }
    }

    public class NerdDinners : DbContext
    {
        
        public NerdDinners(IDbConnection dbconn)            
            :base("abcdefg")
        {
            
        }
        public DbSet<Dinner> Dinners { get; set; }
        public DbSet<RSVP> RSVPs { get; set; }
        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //modelBuilder.Configurations;
            base.OnModelCreating(modelBuilder);
        }
    }


 public NerdDinners(IDbConnection dbconn)  
  :base("abcdefg")
  {
   
  }
这里调用 DataContext(string str) 这个构造函数,
msdn上说:
Constructs a new context instance using the given string as the name or connection string for the database to which a connection will be made. For more information on how this is used to create a connection, see the remarks section for DbContext.

注意这个DataContext 必须是 EF 4.1 上的(必须安装EF 4.1)

然后我是这么写的:name="NerdDinners" connectionString="Data Source=Memory;Pooling=true;FailIfMissing=false" providerName="System.Data.SQLite"

然后执行,到了
 nerdDinners.Dinners.Add(dinner);
也就是第一次使用,就有异常了!
杯具啊!!!!

我估计你们也不太会,死马当活马医。
有人说要有支持 .net 4.1 的 System.Data.SQLite
但是我看了很多国外的帖子,似乎都跑的好好的,怪事哩!!!!!!!

有牛人指点一二吗,谢谢~~~~~~~~

------解决方案--------------------
学习了,帮顶!
------解决方案--------------------
EFCodeFirst 官方并没有说支持 SQLite。
------解决方案--------------------
System.Data.SQLite目前不支持EF4.1 CodeFirst
你可以下载Devart的dotConnect for SQLite,它是支持EF4.1的,不过它是商业license。你也可以用sql compact 4.0, sqlce也是免费不需要安装的,或者直接用sql server来进行测试,相信过一段时间SQLite.net的新版本就会出来,到时候换下驱动就可以了。
------解决方案--------------------
啥 啥 啥 啥
------解决方案--------------------
mango DB似乎能满足你的要求