使用MySQL和 Entity Framework Code First(EF Code First)创建数据库。
1、新建控制台项目ConsoleApp1,使用管理NuGet程序,添加MySql.Data.Entities,MySql.Data.Entities(会自动安装依赖项MySql.Data,EntityFramework)
2、安装程序包以后如下:
3、在app.config中添加如下,如果没有app.config,手动创建一个即可。
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <!--添加mysql连接字符串--> <add name="conn" connectionString="server=localhost;port=3306;uid=root;pwd=mysql@123456;database=testdb;" providerName="MySql.Data.MySqlClient" /> </connectionStrings> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v13.0" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <!--添加Asp.Net EF访问Mysql数据库,否则报错:No Entity Framework provider found for the ADO.NET provider with invariant name 'MySql.Data.MySqlClient'.--> <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" /> </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.8.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> </DbProviderFactories> </system.data> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.8.8.0" newVersion="6.8.8.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
注意:上述红色标记的地方
4、添加数据上下文MyDBContext,代码如下:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity; using System.Text; namespace ConsoleApp1 { public class MyDBContext : DbContext { public DbSet<Testmy> Testmies { get; set; } public MyDBContext(string nameOrConnectionString) : base(nameOrConnectionString) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Testmy>().ToTable($"tbl-{typeof(Testmy)}") .HasKey(a => a.Id) .Property(a => a.Id) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); base.OnModelCreating(modelBuilder); } } public class Testmy { public int Id { get; set; } public string Name { get; set; } } }
5、Program添加如下代码:
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp1 { public class Program { static void Main(string[] args) { using (var db = new MyDBContext("name=conn")) { var isCreate = db.Database.CreateIfNotExists(); for (int i = 0; i < 10; i++) { db.Testmies.Add(new Testmy { Name = $"{i}--名字" }); } db.SaveChanges(); } Console.WriteLine("创建成功"); Console.Read(); } } }
6、运行结果:
下载:使用MySQL和EntityFrameworkCodeFirst(EFCodeFirst)创建数据库。-Microsoft文档类资源-CSDN下载