EntityFramework + MySql Note 2, entityframework
I have just configured the environment and just written several lines of code. I can't wait to run it, duang! Step into the first pit
View code
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(); }
Error message: Specified key was too long; max key length is 767 bytes
Open mysql workbench and check that the created table has been created, but data has not been written. Then try to directly query without writing data, and the error is still the same. No way. Open log, that is, debug. writeLine (log), and find the error.
-- 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);
The last sentence is add primary key. The two fields add up to (150 + 300) * 2 = 900> 767, so an error occurs.
Bing. After searching, find this [C #. NET] [Entity Framework] Decoding Code First @ MySql "Specified key was too long; max key length is 767 bytes and this http://stackoverflow.com/questions/20602114/mysql-connector-6-8-2-rc-entity-framework-6-and-code-first/21120732#21120732 (you must admire stackoverflow, the problem I encountered depends on it)
The solution is simple, that is, the line in the previous code.
[DbConfigurationType (typeof (MySql. Data. Entity. MySqlEFConfiguration)]
Or, in the config file
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
OK. The first pitfall passes smoothly.
Article 1 Configuration