MySql For EF CodeFirst例子

下载MySql Connector

二、添加引用 EntityFramework.dll、MySql.Data.dll、MySql.Data.Entity.dll

三、例子:

    public class ProductContext : DbContext
    {
        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
    }

    public class Category
    {
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
        public virtual ICollection<Product> Products { get; set; }
    }

    public class Product
    {
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public int CategoryId { get; set; }
        public virtual Category Category { get; set; }
    }

四、app.config添加配置节点:

<add connectionString="server=localhost;User Id=root;password=root;database=efdemo;" name="ProductContext" providerName="MySql.Data.MySqlClient"/>

五、验证:

    class Program
    {
        static void Main(string[] args)
        {
            using (var db = new ProductContext())
            {
                var food = new Category { CategoryName = "水果" };
                db.Categories.Add(food);
                int res = db.SaveChanges();
                Console.WriteLine("Saved {0} entities to the database, press any key to exit.", res);
            }

            Console.ReadLine();
        }
    }

参考资料:http://www.cnblogs.com/bikaqiou2000/archive/2012/09/03/2668589.html


六、给Product类添加字段Price,并更新到数据库:

    public class ProductContext : DbContext
    {
        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
    }

    public class Category
    {
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
        public virtual ICollection<Product> Products { get; set; }
    }

    public class Product
    {
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public decimal Price {get;set;}
        public int CategoryId { get; set; }
        public virtual Category Category { get; set; }
    }


(一)打开程序包管理器控制台

工具->库程序包管理器->程序包管理器控制台

二)安装EntityFramework

1、添加EntityFramework包

选择上图中的管理解决方案的NuGet程序包

注:如提示“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。”点击左下角的设置,新建一个可用程序包源:名称:EF官方程序包源;源:http://go.microsoft.com/fwlink/?LinkID=206669,点击确认回到管理NuGet程序包,点击左侧的EF所在程序包,安装EntityFramework。

http:// 

三)更新到数据库

1、打开“程序包管理器控制台”,默认项目选择EF所在的项目,运行Enable-Migrations,(如提示:若要覆盖现有迁移配置,请使用 -Force 参数。运行Enable-Migrations -Force)会在此项目下生成一个文件夹Migrations

2、修改Migrations文件夹下的Configuration.cs文件中AutomaticMigrationsEnabled = false为AutomaticMigrationsEnabled = true;(如提示:未应用自动迁移,因为自动迁移会导致数据丢失。需运行add-migration Initial)

3、运行Update-Database

参考资料:http://blog.csdn.net/cityhunter172/article/details/8062420

     http://blog.csdn.net/qq289523052/article/details/8669516

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值