This two-day all right, even after all the previous research EF mssql Microsoft's own son, replaced by mysql one hundred issues, following the record about how to use EF code first + mysql database;
Project, nuget first introduced EF extension;
If the local EF has expanded, you can use the package management console installed, the command is as follows.
PM> Install-Package EntityFramework -Version 6.0.0
Then nuget MySql.Data.Entity install this extension is to support EF's mysql
I am here to introduce the latest version is 6.10.9;
After the introduction of extended, VS will automatically generate the app.config profile;
<?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> <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.10.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"> </provider> </providers> </entityFramework>
<! - under the new connection string ->
< connectionStrings >
<add name="DataAccess" connectionString="Datasource=127.0.0.1;port=3306;Database=zyturo;uid=root;pwd=fubing;" providerName="MySql.Data.MySqlClient" /> </connectionStrings> </configuration>
Link configuration strings;
<connectionStrings> <add name="DataAccess" connectionString="Datasource=127.0.0.1;port=3306;Database=zyturo;uid=root;pwd=fubing;" providerName="MySql.Data.MySqlClient" /> </connectionStrings>
Creating a database and mapping the relationship between the entity classes
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ZY.Tour.EntityFramework { public class TB_Admin { /// <summary> /// 管理员ID /// </summary> [Key] public Int32 Admin_ID { get; set; } /// <summary> /// 管理员登录账号 /// </ Summary> [the Required] public String the Account { GET ; SET ;} /// <Summary> /// administrator password /// </ Summary> [the Required] public String Password { GET ; SET ;} // / <Summary> /// encryption salts /// </ Summary> [the Required] public String salt { GET ; SET ;} /// <Summary> /// creation time /// </ Summary> public Nullable <the DateTime> {CreateTime GET ; SET ;} /// <Summary> /// help registrant ID /// </ Summary> public Int32 ParentID { GET ; SET ;} / // <the Summary> /// last login IP /// </ the Summary> public String LastLoginIP { GET ; the SET ;} /// <the Summary> /// administrator's department ID /// </ the Summary> public Int32 department_ID {get; set; } } }
Add the characteristics of the entity class attributes ( the Attribute) can refer to this particular description Park Friends of the article: https://www.cnblogs.com/dotnet261010/p/9111954.html
[Key] represents the primary key, he is automatically incremented from zero;
[The Required] expressed can not be empty
Need to use additional features like tagging, you can see which properties are defined under System.ComponentModel.DataAnnotations namespace in the Object Browser;
Here is a DataAccess class I created for the operation of the database;
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ZY.Tour.EntityFramework { [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] public class DataAccess : DbContext { public DataAccess() : base("name=DataAccess") { } public virtual DbSet<TB_Admin> Admin { get; set; } } }
[DbConfigurationType (typeof (MySql.Data.Entity.MySqlEFConfiguration))]
on DbContent class adds an annotation, to declare the data type of living become mysql;
At this point you can normally use, I set up a single member of the test items, test by;
public void TestMethod1() { var da = new DataAccess(); da.Admin.Add(new TB_Admin { Account = "fubing", CreateTime = DateTime.Now, Password = "123123", Salt = "123123" }); da.SaveChanges(); Assert.IsTrue(true); }
The database is created automatically and create the table and data;
Well, the record is completed;