国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

在EF中使用MySQL的方法及常見問題

2024-07-24 13:09:55
字體:
供稿:網(wǎng)友

有時(shí)需要在網(wǎng)上租用空間或數(shù)據(jù)庫,Mysql成本低一些,所以想將sql server轉(zhuǎn)成mysql……

注意:在安裝Mysql時(shí)要選擇文字集為utf8,否則將不能使用中文(當(dāng)前也可以在創(chuàng)建數(shù)據(jù)庫時(shí)使用utf8,不過我不知道在ef生成數(shù)據(jù)庫時(shí)如何設(shè)置,希望高手指點(diǎn))

一、在項(xiàng)目中引用mysql的EF包

通過NuGet包管理器安裝:EntityFramework6.1.3、MySql.Data.Entity6.9.8

也可以用nuget的命令行加入:

Install-Package MySql.Data.Entity

二、新建相關(guān)類

1、新建 User 實(shí)體類

并定義實(shí)例的字段長度,不定義的話會(huì)出現(xiàn)Specified key was too long;max key length is 767 bytes 的錯(cuò)誤,這是因?yàn)閟tring 類型直接映射到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進(jìn)行實(shí)現(xiàn) [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、寫測(cè)試代碼

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());var context = new MyContext();//插入一行值context.Users.Add(new User { UserName = "EF6MySQL" });context.SaveChanges(); 

三、配置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> 

最后,運(yùn)行程序,完成數(shù)據(jù)庫自動(dòng)創(chuàng)建

常見問題

•出現(xiàn)錯(cuò)誤提示: Specified key was too long;max key length is 767 bytes

1)查看實(shí)體的字符串類型屬性是否設(shè)置了長度

2)MyContext 類中是否聲明為生成為mysql 數(shù)據(jù)類型的 [DbConfigurationType(typeof(MySqlEFConfiguration))]

•出現(xiàn)錯(cuò)誤提示: Model compatibility cannot be checked because the database does not contain model metadata

刪除已生成的數(shù)據(jù)庫后重新運(yùn)行程序

•出現(xiàn)錯(cuò)誤提示:序列不包含任何匹配元素

檢查一下:

例如: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; }這個(gè)外鍵設(shè)置。 

2.[Column(TypeName="VARCHAR(254)")] public string ColumnName { get; set; } 這樣的定義,改成: [MaxLength(254)] [Column(TypeName="VARCHAR")] public string ColumnName { get; set; }3.(以下代碼未測(cè)試,因?yàn)槲也皇沁@樣用的,在下篇文章中將進(jìn)行測(cè)試)

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的方法及常見問題的全部敘述,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VeVb武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到MYSQL教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 北流市| 黔南| 沙坪坝区| 遵化市| 胶南市| 菏泽市| 泗洪县| 长子县| 屏东市| 库伦旗| 靖江市| 凌源市| 江门市| 循化| 宣恩县| 古交市| 康乐县| 两当县| 忻州市| 临安市| 龙州县| 清河县| 连江县| 绥滨县| 呼图壁县| 慈溪市| 保靖县| 海原县| 政和县| 沧源| 浦江县| 夏河县| 安龙县| 霞浦县| 嵊泗县| 镇原县| 娄烦县| 民和| 平泉县| 大英县| 嘉禾县|