2014-09-24 20 views
7

我使用的EntityFramework其使用MySQL數據庫6在我的C#模型的第一項目。 一切都很好,我可以生成我的數據庫沒有問題。實體框架MySQL的TINYINT(1)System.Boolean.Parse出現FormatException

然後我使用設計器修改了我的.edmx文件,這裏開始了我遇到的問題。

  • 首先,設計師不會再更新CSDL內容C-S映射內容的.edmx文件的部分。 因此我自己更新了內容,最終可以編譯該項目。

這裏的的.edmx文件,因爲它是現在和它是什麼樣子的設計師

EDMX文件:http://pastebin.com/Xer9UyNR

這裏是鏈接,設計師觀點: http://i.stack.imgur.com/Vcv9W.png

  • 二(也是最重要的一個),我得到的時候EF試圖獲得一個TINYINT從我的數據庫來一個出現FormatException和將其類型更改爲布爾值。
 
    à ArmoireOutils.App.OnNavigateMessageHandler(OnNavigateMessage message) dans c:\Users\JB\Desktop\CodingFrance\ArmoireOutils\ArmoireOutils\App.xaml.cs:line 101System.FormatException: String was not recognized as a valid Boolean.. 
    à System.Boolean.Parse(String value) 
    à System.String.System.IConvertible.ToBoolean(IFormatProvider provider) 
    à System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) 
    à MySql.Data.Entity.EFMySqlDataReader.ChangeType(Object sourceValue, Type targetType) 
    à MySql.Data.Entity.EFMySqlDataReader.GetValue(Int32 ordinal) 
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetUntypedValueDefault(DbDataReader reader, Int32 ordinal) 
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) 
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName) 
    à lambda_method(Closure , Shaper) 
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet) 
    à lambda_method(Closure , Shaper) 
    à System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) 
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.RowNestedResultEnumerator.MaterializeRow() 
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.RowNestedResultEnumerator.MoveNext() 
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.TryReadToNextElement() 
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.ReadElement() 
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.MoveNext() 
    à System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() 
    à System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source) 
    à System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2[TResult](IEnumerable`1 sequence) 
    à System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot) 
    à System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression) 
    à System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression) 
    à System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source) 
    à ArmoireOutils.Services.DataService.GetCupboardByGuid(String guid) dans c:\Users\JB\Desktop\CodingFrance\ArmoireOutils\ArmoireOutils\Services\DataService.cs:line 202 

這裏是我的GetCupboardByGUID方法:

public Cupboard GetCupboardByGuid(String guid) 
    { 
     using (var context = new ArmoireOutilsEntities()) 
     { 
      var cupboard = (from a in context.Cupboards 
       where a.GUID.Equals(guid) 
       select a) 
       .Include("ResidentTools") 
       .Include("Tools") 
       .Include("Users") //If I remove this, .SingleOrDefault() works fine. 
       .SingleOrDefault(); //Throw FormatException when getting the User.Active value from the database. 

      if (cupboard != null) 
       cupboard.RefreshLists(); 

      return cupboard; 
     } 
    } 

,這裏是由TT的.edmx產生我的用戶等級:

public partial class User 
{ 
    public User() 
    { 
     this.Tools = new ObservableCollection<Tool>(); 
     this.Cupboards = new ObservableCollection<Cupboard>(); 
     this.Active = true; 
    } 

    public int Id { get; set; } 
    public short Type { get; set; } 
    public string Firstname { get; set; } 
    public string LastName { get; set; } 
    public string Login { get; set; } 
    public short Gender { get; set; } 
    public short LangId { get; set; } 
    public string Photo { get; set; } 
    public System.DateTime CreationDate { get; set; } 
    public Nullable<System.DateTime> ModificationDate { get; set; } 
    public Nullable<System.DateTime> LastConnection { get; set; } 
    public Nullable<System.DateTime> DisableDate { get; set; } 
    public bool Active { get; set; } 

    public virtual Lang Lang { get; set; } 
    public virtual IList<Tool> Tools { get; set; } 
    public virtual IList<Cupboard> Cupboards { get; set; } 
} 

所以我想EF被迭代來自DB的所有用戶在櫥櫃用戶(t他表用戶鏈接到櫥櫃的許多一對多的關係),當它涉及到設置活動值的第一個用戶,它從數據庫中得到它作爲一個字符串得到1首先嚐試使用System.Boolean.Parse將該字符串解析爲布爾值,但是thaat方法不支持「1」爲真的數字(DB中的字段爲tinyint(1))。

那麼爲什麼EF無法理解它是tinyint,所以他不能在System.Boolean.Parse中使用它?

我試圖從數據庫中重新生成整個的.edmx文件=>相同的異常

我試圖重新生成整個。EDMX文件從頭開始=>相同的異常

我不明白爲什麼,因爲我沒有修改用戶模型,以便在活動場已經在那裏了,並正在就好了。

對不起,很長的職位,並在此先感謝。

最好的問候, theCivilian

+0

您必須告訴MySql在連接字符串中將tinyint作爲布爾值對待。 – 2014-09-24 19:51:34

+0

@GertArnold,感謝您的建議,但不幸的是它說**「不支持'將'tiny'作爲布爾'關鍵字」。**根據文檔,此值默認爲** true **。 (我正在使用** MySQL連接器6.8.3 ** – tehCivilian 2014-09-25 08:02:14

回答

10

配置上的特定實體的數據類型:

modelBuilder.Entity<User>() 
        .Property(p => p.Active) 
        .HasColumnType("bit"); 

或一般:

modelBuilder.Properties() 
      .Where(x => x.PropertyType == typeof(bool)) 
      .Configure(x => x.HasColumnType("bit")); 
+0

已確認可以爲我工作! – Jogai 2016-03-08 11:31:56

+0

如果我們先做數據庫,有沒有辦法做到這一點?不知道在哪裏應用更改。 – sobelito 2016-06-23 03:13:38

0

如果你正在做這個DB頭,簡單地改變TINYINT (1)類型到BIT(1),假設你真的想要一個布爾值。您可能還必須更新默認值(比特語法,如b'0')。 EF仍然會將這些轉換爲實體中的布爾值。