Say just configure the environment, just wrote a few lines of code, can't wait to run, duang! entered the first pit.
Look at the code
Static voidMain (string[] args) {Database.setinitializer (NewDropcreatedatabasealways<mycontext>()); varContext =NewMycontext (); Context. Database.Log= (log) = ={Debug.WriteLine (log);}; Context. Datas.add (NewData{name="Ef6-mysql"}); ( fromOinchContext. DatasSelecto). ToList (); Context. SaveChanges (); }
Error message:Specified key was too long; Max key length is 767 bytes
Open MySQL Workbench and see that the created table is created, but the data has not been written yet. Then try not to write the data directly query, still the same error. No way, open log, is Debug.WriteLine (log) that line, found the wrong place
-- MySql script/6/9: $CREATE TABLE ' __ Migrationhistory ' ( ' migrationid ' nvarchar () not NULL, ' contextkey ' nvarchar ( - Not null, ' Model ' longblob not NULL, ' productversion ' nvarchar (+) not NULL); ALTER TABLE ' __migrationhistory ' ADD PRIMARY KEY (Migrationid, contextkey);
The last sentence is add primary key, two fields add up is (150+300) *2=900 > 767, so the error.
Bing, after the search, find this [c#.net][entity Framework] To resolve 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-framewo rk-6-and-code-first/21120732#21120732 (must admire the StackOverflow, I met the problem basically rely on it)
The solution is simple, a 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 pit passed smoothly.
First article Configuration
Entityframework+mysql Notes 2