我正在尝试使用Entity Framework 6 Database First来为现有的MySQL表生成模型。我不允许改变。
表创建脚本如下所示:
CREATE TABLE `ht_homes` (
`id` char(36) NOT NULL,
`zpid` varchar(255) DEFAULT NULL,
`type_info` varchar(255) DEFAULT NULL,
`city` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
`zipcode` varchar(255) DEFAULT NULL,
`price` decimal(26,6) DEFAULT NULL,
`agent_name` varchar(255) DEFAULT NULL,
`agent_phone` varchar(255) DEFAULT NULL,
`agent_type` varchar(255) DEFAULT NULL,
`zestimate` decimal(26,6) DEFAULT NULL,
`zestimate_range_low` decimal(26,6) DEFAULT NULL,
`zestimate_range_high` decimal(26,6) DEFAULT NULL,
`zestimate_rental` varchar(255) DEFAULT NULL,
`zestimate_median` decimal(26,6) DEFAULT NULL,
`area` varchar(255) DEFAULT NULL,
`lot` varchar(255) DEFAULT NULL,
`location` varchar(255) DEFAULT NULL,
`url` varchar(255) DEFAULT NULL,
`dt` datetime DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`date_entered` datetime DEFAULT NULL,
`date_modified` datetime DEFAULT NULL,
`modified_user_id` char(36) DEFAULT NULL,
`created_by` char(36) DEFAULT NULL,
`description` text,
`deleted` tinyint(1) DEFAULT '0',
`assigned_user_id` char(36) DEFAULT NULL,
`address_state` varchar(100) DEFAULT NULL,
`address_country` varchar(100) DEFAULT NULL,
`bid_price` decimal(26,6) DEFAULT NULL,
`bid_price_calculated` tinyint(1) DEFAULT '0',
`bid_price_attempted` tinyint(1) DEFAULT '0',
`davis_api_data_json` text,
`offer_calculated` tinyint(1) DEFAULT '1',
`dif_h_l` decimal(26,6) DEFAULT NULL,
`dif_m_l` decimal(26,6) DEFAULT NULL,
`dif_z_l` decimal(26,6) DEFAULT NULL,
`offer_email` tinyint(1) DEFAULT '0',
`last_sold_price` decimal(26,6) DEFAULT NULL,
`agent_assigned` tinyint(1) DEFAULT '0',
`agent_attempted` tinyint(1) DEFAULT '0',
`offer_sent_status` text,
`offer_sent_time` datetime DEFAULT NULL,
`counter_offer` decimal(26,6) DEFAULT NULL,
`offer_attempted` tinyint(1) DEFAULT '0',
`build_in` int(255) DEFAULT NULL,
`hoa_fee_frequency` varchar(255) DEFAULT NULL,
`sewage_system` text,
`garage_parking_info` text,
`property_type` varchar(255) DEFAULT NULL,
`baths_half` int(11) DEFAULT NULL,
`county_or_parish` varchar(255) DEFAULT NULL,
`basement_description` text,
`preferred_phone` varchar(255) DEFAULT NULL,
`foundation_type` text,
`mo_lease_amount` int(11) DEFAULT NULL,
`tax_district_amount` int(11) DEFAULT NULL,
`mil_rate_total` decimal(20,4) DEFAULT NULL,
`current_price` decimal(20,4) DEFAULT NULL,
`exterior_siding` text,
`condo_model_name` varchar(255) DEFAULT NULL,
`expiration_date` datetime DEFAULT NULL,
`showing_instructions` varchar(255) DEFAULT NULL,
`short_sale_comments` varchar(255) DEFAULT NULL,
`year_built` int(11) DEFAULT NULL,
`heat_fuel_type` text,
`room_count` int(11) DEFAULT NULL,
`style` text,
`street_dir_prefix` varchar(255) DEFAULT NULL,
`zestimate_renthigh` varchar(255) DEFAULT NULL,
`baths_total` decimal(20,4) DEFAULT NULL,
`flood_zone_yn` varchar(255) DEFAULT NULL,
`public_remarks` text,
`sq_ft_total` int(11) DEFAULT NULL,
`list_agent_mui` int(11) DEFAULT NULL,
`water_source` text,
`status` varchar(255) DEFAULT NULL,
`fuel_tank_location` varchar(255) DEFAULT NULL,
`owner_phone` varchar(255) DEFAULT NULL,
`parking_total_spaces` int(11) DEFAULT NULL,
`baths_full` int(11) DEFAULT NULL,
`in_law_apartment_ynp` varchar(255) DEFAULT NULL,
`waterfront_description` text,
`street_number` varchar(255) DEFAULT NULL,
`hot_water_description` text,
`elementary_school` varchar(255) DEFAULT NULL,
`zillow_parse` int(11) DEFAULT NULL,
`list_agent_full_name` varchar(255) DEFAULT NULL,
`heat_type` text,
`close_price` decimal(20,4) DEFAULT NULL,
`complex_name` varchar(255) DEFAULT NULL,
`bank_owned_property_yn` varchar(255) DEFAULT NULL,
`energy_features` text,
`last_change_timestamp` datetime DEFAULT NULL,
`swimming_pool_yn` varchar(255) DEFAULT NULL,
`mls_number` varchar(255) DEFAULT NULL,
`pool_description` text,
`cooling_system` text,
`zestimate_lastsoldprice` varchar(255) DEFAULT NULL,
`fireplaces_total` int(11) DEFAULT NULL,
`other_phone_number` varchar(255) DEFAULT NULL,
`new_construction_type` varchar(255) DEFAULT NULL,
`street_name` varchar(255) DEFAULT NULL,
`property_sub_type` varchar(255) DEFAULT NULL,
`property_tax` int(11) DEFAULT NULL,
`remarks_public_addendum` text,
`list_office_name` varchar(255) DEFAULT NULL,
`sq_ft_source` varchar(255) DEFAULT NULL,
`zestimate_rentamount` varchar(255) DEFAULT NULL,
`beds_total` int(11) DEFAULT NULL,
`median_list_price` decimal(20,0) DEFAULT NULL,
`hoa_fee_amount` int(11) DEFAULT NULL,
`matrix_modified_dt` datetime DEFAULT NULL,
`mls` varchar(255) DEFAULT NULL,
`zestimate_rentlow` varchar(255) DEFAULT NULL,
`postal_code_plus4` varchar(255) DEFAULT NULL,
`direct_waterfront_yn` varchar(255) DEFAULT NULL,
`last_status` varchar(255) DEFAULT NULL,
`rooms_total` int(11) DEFAULT NULL,
`total_numof_units` int(11) DEFAULT NULL,
`zestimate_low` varchar(255) DEFAULT NULL,
`garages_number` int(11) DEFAULT NULL,
`close_date` datetime DEFAULT NULL,
`purchase_option_yn` varchar(255) DEFAULT NULL,
`subdivision` varchar(255) DEFAULT NULL,
`list_agent_mlsid` varchar(255) DEFAULT NULL,
`street_suffix` varchar(255) DEFAULT NULL,
`utilities_available` text,
`list_office_mlsid` varchar(255) DEFAULT NULL,
`matrix_unique_id` int(11) DEFAULT '0',
`zillow_home_url` varchar(255) DEFAULT '0',
`zestimate_high` varchar(255) DEFAULT NULL,
`rooms_total_multi_family` int(11) DEFAULT NULL,
`lease_purchase_yn` varchar(255) DEFAULT NULL,
`zestimate_lastsolddate` varchar(255) DEFAULT NULL,
`state_or_province` varchar(255) DEFAULT NULL,
`street_number_numeric` int(11) DEFAULT NULL,
`owner_name` varchar(255) DEFAULT NULL,
`last_change_type` varchar(255) DEFAULT NULL,
`unit_number` varchar(255) DEFAULT NULL,
`high_school` varchar(255) DEFAULT NULL,
`street_no_extension` varchar(255) DEFAULT NULL,
`possession_availability` varchar(255) DEFAULT NULL,
`status_change_timestamp` datetime DEFAULT NULL,
`driveway_type` text,
`directions` varchar(255) DEFAULT NULL,
`street_dir_suffix` varchar(255) DEFAULT NULL,
`postal_code` varchar(255) DEFAULT NULL,
`acres` decimal(20,4) DEFAULT NULL,
`zillow_region_id` varchar(255) DEFAULT NULL,
`property_rating` varchar(255) DEFAULT NULL,
`original_entry_timestamp` datetime DEFAULT NULL,
`assessed_value` int(11) DEFAULT NULL,
`original_list_price` decimal(20,4) DEFAULT NULL,
`price_change_timestamp` datetime DEFAULT NULL,
`dom` int(11) DEFAULT NULL,
`lots_number_of` int(11) DEFAULT NULL,
`rets_ct` tinyint(1) DEFAULT '0',
`last_sold_price_list_price` decimal(26,0) DEFAULT NULL,
`median_list_price_list_price` decimal(26,0) DEFAULT NULL,
`status_ct` varchar(255) DEFAULT NULL,
`hud_case_number` varchar(15) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `home` (`zpid`),
KEY `homes` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
这是EF生成的类:
public partial class ht_homes
{
public System.Guid id { get; set; }
public string zpid { get; set; }
public string type_info { get; set; }
public string city { get; set; }
public string address { get; set; }
public string zipcode { get; set; }
public Nullable<decimal> price { get; set; }
public string agent_name { get; set; }
public string agent_phone { get; set; }
public string agent_type { get; set; }
public Nullable<decimal> zestimate { get; set; }
public Nullable<decimal> zestimate_range_low { get; set; }
public Nullable<decimal> zestimate_range_high { get; set; }
public string zestimate_rental { get; set; }
public Nullable<decimal> zestimate_median { get; set; }
public string area { get; set; }
public string lot { get; set; }
public string location { get; set; }
public string url { get; set; }
public Nullable<System.DateTime> dt { get; set; }
public string name { get; set; }
public Nullable<System.DateTime> date_entered { get; set; }
public Nullable<System.DateTime> date_modified { get; set; }
public Nullable<System.Guid> modified_user_id { get; set; }
public Nullable<System.Guid> created_by { get; set; }
public string description { get; set; }
public Nullable<bool> deleted { get; set; }
public Nullable<System.Guid> assigned_user_id { get; set; }
public string address_state { get; set; }
public string address_country { get; set; }
public Nullable<decimal> bid_price { get; set; }
public Nullable<bool> bid_price_calculated { get; set; }
public Nullable<bool> bid_price_attempted { get; set; }
public string davis_api_data_json { get; set; }
public Nullable<bool> offer_calculated { get; set; }
public Nullable<decimal> dif_h_l { get; set; }
public Nullable<decimal> dif_m_l { get; set; }
public Nullable<decimal> dif_z_l { get; set; }
public Nullable<bool> offer_email { get; set; }
public Nullable<decimal> last_sold_price { get; set; }
public Nullable<bool> agent_assigned { get; set; }
public Nullable<bool> agent_attempted { get; set; }
public string offer_sent_status { get; set; }
public Nullable<System.DateTime> offer_sent_time { get; set; }
public Nullable<decimal> counter_offer { get; set; }
public Nullable<bool> offer_attempted { get; set; }
public Nullable<int> build_in { get; set; }
public string hoa_fee_frequency { get; set; }
public string sewage_system { get; set; }
public string garage_parking_info { get; set; }
public string property_type { get; set; }
public Nullable<int> baths_half { get; set; }
public string county_or_parish { get; set; }
public string basement_description { get; set; }
public string preferred_phone { get; set; }
public string foundation_type { get; set; }
public Nullable<int> mo_lease_amount { get; set; }
public Nullable<int> tax_district_amount { get; set; }
public Nullable<decimal> mil_rate_total { get; set; }
public Nullable<decimal> current_price { get; set; }
public string exterior_siding { get; set; }
public string condo_model_name { get; set; }
public Nullable<System.DateTime> expiration_date { get; set; }
public string showing_instructions { get; set; }
public string short_sale_comments { get; set; }
public Nullable<int> year_built { get; set; }
public string heat_fuel_type { get; set; }
public Nullable<int> room_count { get; set; }
public string style { get; set; }
public string street_dir_prefix { get; set; }
public string zestimate_renthigh { get; set; }
public Nullable<decimal> baths_total { get; set; }
public string flood_zone_yn { get; set; }
public string public_remarks { get; set; }
public Nullable<int> sq_ft_total { get; set; }
public Nullable<int> list_agent_mui { get; set; }
public string water_source { get; set; }
public string status { get; set; }
public string fuel_tank_location { get; set; }
public string owner_phone { get; set; }
public Nullable<int> parking_total_spaces { get; set; }
public Nullable<int> baths_full { get; set; }
public string in_law_apartment_ynp { get; set; }
public string waterfront_description { get; set; }
public string street_number { get; set; }
public string hot_water_description { get; set; }
public string elementary_school { get; set; }
public Nullable<int> zillow_parse { get; set; }
public string list_agent_full_name { get; set; }
public string heat_type { get; set; }
public Nullable<decimal> close_price { get; set; }
public string complex_name { get; set; }
public string bank_owned_property_yn { get; set; }
public string energy_features { get; set; }
public Nullable<System.DateTime> last_change_timestamp { get; set; }
public string swimming_pool_yn { get; set; }
public string mls_number { get; set; }
public string pool_description { get; set; }
public string cooling_system { get; set; }
public string zestimate_lastsoldprice { get; set; }
public Nullable<int> fireplaces_total { get; set; }
public string other_phone_number { get; set; }
public string new_construction_type { get; set; }
public string street_name { get; set; }
public string property_sub_type { get; set; }
public Nullable<int> property_tax { get; set; }
public string remarks_public_addendum { get; set; }
public string list_office_name { get; set; }
public string sq_ft_source { get; set; }
public string zestimate_rentamount { get; set; }
public Nullable<int> beds_total { get; set; }
public Nullable<decimal> median_list_price { get; set; }
public Nullable<int> hoa_fee_amount { get; set; }
public Nullable<System.DateTime> matrix_modified_dt { get; set; }
public string mls { get; set; }
public string zestimate_rentlow { get; set; }
public string postal_code_plus4 { get; set; }
public string direct_waterfront_yn { get; set; }
public string last_status { get; set; }
public Nullable<int> rooms_total { get; set; }
public Nullable<int> total_numof_units { get; set; }
public string zestimate_low { get; set; }
public Nullable<int> garages_number { get; set; }
public Nullable<System.DateTime> close_date { get; set; }
public string purchase_option_yn { get; set; }
public string subdivision { get; set; }
public string list_agent_mlsid { get; set; }
public string street_suffix { get; set; }
public string utilities_available { get; set; }
public string list_office_mlsid { get; set; }
public Nullable<int> matrix_unique_id { get; set; }
public string zillow_home_url { get; set; }
public string zestimate_high { get; set; }
public Nullable<int> rooms_total_multi_family { get; set; }
public string lease_purchase_yn { get; set; }
public string zestimate_lastsolddate { get; set; }
public string state_or_province { get; set; }
public Nullable<int> street_number_numeric { get; set; }
public string owner_name { get; set; }
public string last_change_type { get; set; }
public string unit_number { get; set; }
public string high_school { get; set; }
public string street_no_extension { get; set; }
public string possession_availability { get; set; }
public Nullable<System.DateTime> status_change_timestamp { get; set; }
public string driveway_type { get; set; }
public string directions { get; set; }
public string street_dir_suffix { get; set; }
public string postal_code { get; set; }
public Nullable<decimal> acres { get; set; }
public string zillow_region_id { get; set; }
public string property_rating { get; set; }
public Nullable<System.DateTime> original_entry_timestamp { get; set; }
public Nullable<int> assessed_value { get; set; }
public Nullable<decimal> original_list_price { get; set; }
public Nullable<System.DateTime> price_change_timestamp { get; set; }
public Nullable<int> dom { get; set; }
public Nullable<int> lots_number_of { get; set; }
public Nullable<bool> rets_ct { get; set; }
public Nullable<decimal> last_sold_price_list_price { get; set; }
public Nullable<decimal> median_list_price_list_price { get; set; }
public string status_ct { get; set; }
public string hud_case_number { get; set; }
}
我修改了连接字符串以包含:
oldguids =真
当我尝试从数据库中调用一些数据时:
using (suitecrmEntities db = new suitecrmEntities())
{
List<ht_homes> homes = db.ht_homes
.OrderBy(x => x.property_type)
.Skip(0)
.Take(100)
.ToList();
}
我收到错误:
System.InvalidCastException:从'System.String'到'System.Guid'的无效转换
这是堆栈跟踪:
Result StackTrace:
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at MySql.Data.Entity.EFMySqlDataReader.ChangeType(Object sourceValue, Type targetType)
at MySql.Data.Entity.EFMySqlDataReader.GetValue(Int32 ordinal)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetUntypedValueDefault(DbDataReader reader, Int32 ordinal)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
at lambda_method(Closure , Shaper )
at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at SuiteCRM.Data.MySQL.Class1.GetSomeProperties() in d:\Users\Trevor\documents\visual studio 2017\Projects\HudHome\SuiteCRM.Data.MySQL\Class1.cs:line 21
at SuiteCRM.Data.MySQL.Tests.UnitTest1.SuiteCRM_Data_GetSomeProperties() in d:\Users\Trevor\documents\visual studio 2017\Projects\HudHome\SuiteCRM.Data.MySQL.Tests\UnitTest1.cs:line 22
Result Message:
Test method SuiteCRM.Data.MySQL.Tests.UnitTest1.SuiteCRM_Data_GetSomeProperties threw exception:
System.InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'.
有人可以建议一种方法来克服这个问题吗?
我的直觉是,EF正在将桌子翻译成一个不正确的模型,可能是guids。但是,oldguids = True应该克服这一点。
因为它是EF数据库优先我无法更改生成的类,因为如果更新了数据库模型,它将被覆盖。
答案 0 :(得分:0)
是的,非常明显的原因是您的表定义表明id
是char
类型
`id` char(36) NOT NULL,
但您的模型定义为Guid
public System.Guid id { get; set; }
您也应该在模型中将id
定义为string
public string id { get; set; }
答案 1 :(得分:0)
尝试设置oldguid = false。当你的mysql列是Binary(16)类型时,似乎是真的。查看文档here。
此选项在Connector / Net 6.1.1中引入。后端 GUID类型的表示从BINARY(16)更改为CHAR(36)。 这样做是为了允许开发人员使用服务器函数UUID()来实现 填充GUID表 - UUID()生成一个36个字符的字符串。 旧应用程序的开发人员可以添加旧版Guids = true&#39;到了 连接字符串,以使用数据类型为BINARY(16)的GUID。
答案 2 :(得分:0)
首先,有一个MySql
错误会导致此行为
https://bugs.mysql.com/bug.php?id=48417
在连接字符串中添加文本“ old guids = True”应该可以解决问题
如果问题仍然存在,请检查实体框架生成的类的类型为string
而不是Guid
作为最后的资源检查,以查看EF,mysql连接器或mysql服务器是否有任何更新