我在用着实体框架6在我的 C# 中模型优先项目使用了MySQL 数据库。一切都很好,我可以毫无问题地生成数据库。
然后我修改了我的.edmx使用设计器创建文件,这里开始解决我遇到的问题。
- 首先设计师没有更新CSDL内容和C-S映射内容.edmx 文件的各个部分不再存在。
于是我自己更新了内容,终于可以编译项目了。
这是.edmx文件现在的样子以及它在设计器中的样子:
EDMX 文件:http://pastebin.com/Xer9UyNR
这是设计器视图的链接:https://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
这是我的通过GUID获取Cupboard method:
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;
}
}
这是由 .edmx tt 生成的我的 User 类:
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 正在迭代数据库中的所有用户橱柜用户(该表链接了user to a cupboard对于多对多关系)以及设置时Active第一个用户的值,它从数据库中获取 1 作为String首先,然后尝试使用将该字符串解析为布尔值系统.布尔.解析但该方法不支持像“1”这样的数字表示 true(数据库中的字段是微小整数(1)).
那么为什么 EF 无法理解它是一个tinyint,所以他不能在系统.布尔.解析 ?
我试图重新生成整个.edmx来自数据库的文件 => 相同的异常
我试图重新生成整个.edmx从头开始文件 => 相同的异常
我不明白为什么,因为我没有修改 User 模型,所以Active现场已经在那里并且工作得很好。
抱歉发了这么长的帖子,并提前致谢。
此致,
平民