Entityframework+mysql Notes 2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.