EntityFramework+MySql 筆記2

來源:互聯網
上載者:User

標籤:

話說剛剛配置好環境,剛剛寫了幾行代碼,迫不及待地運行,duang! 踏進了第一個坑

看代碼

        static void Main(string[] args)        {                Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());                  var context = new MyContext();                context.Database.Log = (log) => { Debug.WriteLine(log); };                context.Datas.Add(new Data{Name="EF6-MySql"});                 (from o in context.Datas select o).ToList();                 context.SaveChanges();          }

錯誤資訊:Specified key was too long; max key length is 767 bytes

開啟mysql workbench,看到該建立的表已建立,只是還沒有寫入資料。然後嘗試不寫資料直接查詢,仍然同樣錯誤。沒辦法,開啟log,就是 debug.writeLine(log)那行,找到了出錯的地方

-- MySql script-- Created on 2015/6/30 9:46:53CREATE TABLE `__MigrationHistory`(    `MigrationId` nvarchar (150) NOT NULL,     `ContextKey` nvarchar (300) NOT NULL,     `Model` longblob NOT NULL,     `ProductVersion` nvarchar (32) NOT NULL);ALTER TABLE `__MigrationHistory` ADD PRIMARY KEY (MigrationId, ContextKey);

最後一句是 add primary key,兩個欄位加起來是 (150+300)*2=900 > 767 ,所以出錯了。

Bing之 ,在搜尋之後,找到這篇 [C#.NET][Entity Framework] 解決 Code First @ MySql 出現 “Specified key was too long; max key length is 767 bytes    以及這篇    http://stackoverflow.com/questions/20602114/mysql-connector-6-8-2-rc-entity-framework-6-and-code-first/21120732#21120732  (必須讚歎一下stackoverflow,我遇上的問題基本都靠它了)

解決的辦法很簡單,就是上篇代碼中的一行 

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]

或者,在config檔案中

<entityFramework  codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">

OK,第一個坑順利通過。

第一篇 配置

EntityFramework+MySql 筆記2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.