从字符串转换数据库到布尔属性实体框架4.1 [英] Convert from to string in database to boolean property Entity Framework 4.1
问题描述
我正在使用字符文字t和f在数据库中的布尔值不过,我想实体属性,以反映正确的boolen值遗留的Oracle数据库有一个WY转换此值时,该模型结合它是一个只读数据库,以便插入并不重要
i am working on a legacy oracle database that uses character literals T and F in the database for its boolean values however i would like the Entity property to reflect a proper boolen value is there a wy to convert this value when the model is binding it is a read only database so inserts are not important
推荐答案
我建议结束了或延长产生的类型添加这种功能...
I would suggest wrapping up or extending the generating type to add this sort of functionality ...
实体框架将生成基本上相匹配的数据库表中的数据看起来像这样,如果你有一个名为联系人,你会得到一个对象调用联系人表对象,我认为(虽然我可能是错的)这些类被定义为局部因此产生类似...
entity framework will generate objects that basically match what the data in the database tables looks like so if you have a table called "Contacts" you'll get an object called "Contacts", I think (although i could be wrong) the classes are defined as partial so it generates something like ...
public partial class Contact {
string BoolReally {
get; set;
};
}
您再添加一个新的属性像这样...
you then add a new property like this ...
public partial class Contact {
bool MyBool get {
return (legacyValue == "T") ? true : false;
}
}
现在,当你宣布一个联系的实例就是从MyBool,而不是鱼的价值。
Now when you declare a Contact instance just fish the value from "MyBool" instead.
...
这就是延伸,结束了会是这样的...
That's extending, wrapping up would be something like this ...
public class MyContact {
public Contact Contact;
public bool MyBool {
get {
return Contact.BoolAsString;
}
}
}
类似的事情......只是需要消耗对象略有不同:)
similar thing ... just need to consume the object slightly differently :)
这篇关于从字符串转换数据库到布尔属性实体框架4.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!