前面有说明使用EF动态新建数据库与表,数据库使用的是SQL SERVER2008的,在使用MYSQL的时候还是有所不同
一、添加 EntityFramework.dll ,System.Data.Entity.dll ,MySql.Data, MySql.Data.Entity.EF6
注意:Entity Framework5.0.0(好像最新的6.X是不支持mysql的)
二、添加dll文件,可以使用“管理NuGet程序包”或者“Nuget程序包管理控制台”添加
与 控制台平台添加
以下为控制台平台安装dll语句
EntityFramework.dll : Install-Package EntityFramework -Version 5.0.0
MySql.Data : Install-Package MySql.Data -Version 6.9.12
MySql.Data.Entity:Install-Package MySql.Data.Entity -Version 6.9.12
三、在app.config文件中添加connectionStrings
源文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?xml version= "1.0" encoding= "utf-8" ?> <configuration> <configSections> <section name= "entityFramework" type= "System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission= "false" /> <!-- For more information on Entity Framework configuration, visit http: //go.microsoft.com/fwlink/?LinkID=237468 --> </configSections> <entityFramework> <defaultConnectionFactory type= "System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> <providers> <provider invariantName= "System.Data.SqlClient" type= "System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName= "MySql.Data.MySqlClient" type= "MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.12.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" ></provider> </providers> </entityFramework> <system.data> <DbProviderFactories> <remove invariant= "MySql.Data.MySqlClient" /> <add name= "MySQL Data Provider" invariant= "MySql.Data.MySqlClient" description= ".Net Framework Data Provider for MySQL" type= "MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.12.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> </DbProviderFactories> </system.data> <connectionStrings> <!--<add name= "conncodefirst" connectionString= "server=127.0.0.1;port=3306;uid=root;pwd=repower;database=code" providerName= "MySql.Data.MySqlClient" />--> <add name= "conncodefirst" connectionString= "server=127.0.0.1;user id=root;password=repower;database=code" providerName= "MySql.Data.MySqlClient" /> </connectionStrings> </configuration> |
四、添加类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | [Table( "Student" )] public class Student { public Student() { } [Key] public int StudentID { get ; set ; } public string StudentName { get ; set ; } public DateTime AddTime { get ; set ; } } [Table( "Standard" )] public class Standard { public Standard() { } public int StandardId { get ; set ; } public string StandardName { get ; set ; } public DateTime AddTime { get ; set ; } } |
添加SchoolContext 继承DbContext
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | [DbConfigurationType( typeof (MySql.Data.Entity.MySqlEFConfiguration))] public class SchoolContext : DbContext { public SchoolContext() : base ( "name=conncodefirst" ) { } static SchoolContext() { DbConfiguration.SetConfiguration( new MySql.Data.Entity.MySqlEFConfiguration()); } public DbSet<Student> Students { get ; set ; } public DbSet<Standard> Standards { get ; set ; } //protected override void OnModelCreating(DbModelBuilder modelBuilder) //{ // base.OnModelCreating(modelBuilder); //} } |
最后调用
1 2 3 4 5 6 | SchoolContext dbCOntext = new SchoolContext(); //dbCOntext.Database. bool flag = dbCOntext.Database.CreateIfNotExists(); Student stud = new Student() { StudentName = "New Student" , AddTime = DateTime.Now }; dbCOntext.Students.Add(stud); dbCOntext.SaveChanges(); |
结果
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· C# 13 中的新增功能实操
· Supergateway:MCP服务器的远程调试与集成工具