【发布时间】:2014-09-24 15:50:30
【问题描述】:
我在使用 MySQL 数据库的 C# model-first 项目中使用 EntityFramework 6。 一切都很好,我可以毫无问题地生成我的数据库。
然后我使用设计器修改了我的 .edmx 文件,这里开始出现我遇到的问题。
- 首先,设计者不再更新 .edmx 文件的 CSDL 内容 和 C-S 映射内容 部分。 于是我自己更新了内容,终于可以编译项目了。
这是 .edmx 文件,它现在的样子以及它在设计器中的样子:
EDMX 文件:http://pastebin.com/Xer9UyNR
这里是设计器视图的链接: http://i.stack.imgur.com/Vcv9W.png
- 第二个(也是最重要的一个),当 EF 尝试从我的数据库中获取一个 tinyint 并将其类型更改为布尔值时,我得到一个 FormatException。
这是我的 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;
}
}
这是由 .edmx tt 生成的我的用户类:
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 正在遍历数据库中在 cupboarduser 中的所有用户(将 user 链接到 cupboard 的表对于多对多关系),当为第一个用户设置 Active 值时,它会从数据库中获取 1,首先将其作为 String 并然后尝试使用 System.Boolean.Parse 将该字符串解析为布尔值,但该方法不支持像“1”这样的数字为真(数据库中的字段是 tinyint(1 ))。
那么为什么 EF 无法理解它是一个 tinyint,所以他不能在 System.Boolean.Parse 中使用它?
我试图从数据库中重新生成整个 .edmx 文件 => 相同的异常
我尝试从头开始重新生成整个 .edmx 文件 => 相同的异常
我不明白为什么,因为我没有修改 User 模型,所以 Active 字段已经存在并且工作正常。
抱歉发了这么长的帖子,提前感谢。
最好的问候, 平民
【问题讨论】:
-
你必须告诉 MySql 在连接字符串中将 tinyint 视为布尔值。
-
@GertArnold,感谢您的建议,但不幸的是它说 “不支持 'treat tiny as boolean' 关键字。” 这个值也是 true 默认情况下根据文档。 (我正在使用 MySQL-Connector 6.8.3
标签: c# mysql entity-framework visual-studio-2013