EF6串連MySQL若干問題匯總

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   sp   for   on   資料   

問題1. 中文亂碼問題

連接字串已經設定charset=utf8,但依舊出現亂碼,解決方案:配置字串欄位約定支援Unicode

protected override void OnModelCreating(DbModelBuilder modelBuilder){    modelBuilder.Properties<string>().Configure(x => x.IsUnicode(true));}

問題2. 資料庫串聯刪除問題

拋出的異常類似於“Introducing FOREIGN KEY constraint ‘‘ on table ‘‘ may cause cycles or multiple cascade paths.”,解決方案:刪除預設的一對多串聯刪除約定

protected override void OnModelCreating(DbModelBuilder modelBuilder){    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();}

問題3. 定義唯一約束

EF6中支援定義唯一約束,方法:對需要被定義為唯一約束的屬性添加索引特性。

[System.ComponentModel.DataAnnotations.Schema.Index(IsUnique = true)]public string Code { get; set; }

問題4. __Migrationhistory表中主鍵超出最大長度問題

自訂資料庫配置類,該類繼承於DbConfiguration,屬於全域配置類,代碼如下:

public class MySqlConfiguration : DbConfiguration{    public MySqlConfiguration()    {        SetHistoryContext("MySql.Data.MySqlClient", (conn, schema) => new MySqlHistoryContext(conn, schema));    }}public class MySqlHistoryContext : HistoryContext{    public MySqlHistoryContext(DbConnection existingConnection, string defaultSchema)        : base(existingConnection, defaultSchema)    {    }    protected override void OnModelCreating(DbModelBuilder modelBuilder)    {        base.OnModelCreating(modelBuilder);        modelBuilder.Entity<HistoryRow>().Property(h => h.MigrationId).HasMaxLength(100).IsRequired();        modelBuilder.Entity<HistoryRow>().Property(h => h.ContextKey).HasMaxLength(200).IsRequired();    }}

EF6串連MySQL若干問題匯總

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.