在EF中應用MySQL的辦法及罕見成績。本站提示廣大學習愛好者:(在EF中應用MySQL的辦法及罕見成績)文章只能為提供參考,不一定能成為您想要的結果。以下是在EF中應用MySQL的辦法及罕見成績正文
有時須要在網上租用空間或數據庫,Mysql本錢低一些,所以想將sql server轉成mysql……
留意:在裝置Mysql時要選擇文字集為utf8,不然將不克不及應用中文(以後也能夠在創立數據庫時應用utf8,不外我不曉得在ef生成數據庫時若何設置,願望高手指導)
1、在項目中援用mysql的EF包
經由過程NuGet擔保理器裝置:EntityFramework6.1.3、MySql.Data.Entity6.9.8
也能夠用nuget的敕令行參加:
Install-Package MySql.Data.Entity
2、新建相干類
1、新建 User 實體類
並界說實例的字段長度,不界說的話會湧現Specified key was too long;max key length is 767 bytes 的毛病,這是由於string 類型直接映照到mysql 中的話是longtext,而mysql 支撐最年夜長度為767 bytes.
public class User { public int Id { get; set; } [StringLength(30)] public string UserName { get; set; } [MaxLength(30)] public string PassWord { get; set; } }
2、新建 MyContext 類
並解釋用MySql停止完成 [DbConfigurationType(typeof(MySqlEFConfiguration))]
[DbConfigurationType(typeof(MySqlEFConfiguration))] public class MyContext : DbContext { public MyContext() : base("name=MyContext")//web.config中connectionstring的名字 { } public DbSet<User> Users { get; set; } }
3、寫測試代碼
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>()); var context = new MyContext(); //拔出一行值 context.Users.Add(new User { UserName = "EF6MySQL" }); context.SaveChanges();
3、設置裝備擺設Web.config
在<connectionStrings>中參加以下代碼:
<add name="MyContext" connectionString="Data Source=localhost;port=3306;Initial Catalog=MySQL_EF;user id=root;password=root;" providerName="MySql.Data.MySqlClient" />
完全的web.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> <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.8.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.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> </DbProviderFactories> </system.data> <connectionStrings> <add name="MyContext" connectionString="Data Source=localhost;port=3306;Initial Catalog=MySQL_EF;user id=root;password=root;" providerName="MySql.Data.MySqlClient" /> </connectionStrings> </configuration>
最初,運轉法式,完成數據庫主動創立
罕見成績
•湧現毛病提醒: Specified key was too long;max key length is 767 bytes
1)檢查實體的字符串類型屬性能否設置了長度
2)MyContext 類中能否聲明為生成為mysql 數據類型的 [DbConfigurationType(typeof(MySqlEFConfiguration))]
•湧現毛病提醒: Model compatibility cannot be checked because the database does not contain model metadata
刪除已生成的數據庫後從新運轉法式
•湧現毛病提醒:序列不包括任何婚配元素
檢討一下:
例如:1.
public class Employee { [Key] public int EmployeeId { get; set; } public string Name { get; set; } [ForeignKey("ManagerId")] public Employee Manager { get; set; } public int ManagerId { get; set; } }[ForeignKey("ManagerId")] public Employee Manager { get; set; } public int ManagerId { get; set; }這個外鍵設置。
2.[Column(TypeName="VARCHAR(254)")] public string ColumnName { get; set; } 如許的界說,改成: [MaxLength(254)] [Column(TypeName="VARCHAR")] public string ColumnName { get; set; }3.(以下代碼未測試,由於我不是如許用的,鄙人篇文章中將停止測試)
modelBuilder.Entity<Category>() .HasKey(c => c.IdCategory ) .HasOptional(p => p.Children) .WithMany() .HasForeignKey(c => c.ChildrenId);
改成:
modelBuilder.Entity<Category>() .HasKey(c => c.IdCategory ) .HasMany(p => p.Children) .WithOptional() .HasForeignKey(c => c.ChildrenId);
.WithMany()換成.WithOptional()
以上所述是小編給年夜家引見的在EF中應用MySQL的辦法及罕見成績的全體論述,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!