EntityFramework + MySql Note 2, entityframework

Source: Internet
Author: User

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

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.