C# 实体框架-MySQL-从';System.String';至';System.Guid';

C# 实体框架-MySQL-从';System.String';至';System.Guid';,c#,mysql,entity-framework,C#,Mysql,Entity Framework,我试图首先使用EntityFramework6数据库为现有的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) DEFAUL

我试图首先使用EntityFramework6数据库为现有的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;
using (suitecrmEntities db = new suitecrmEntities())
        {
            List<ht_homes> homes = db.ht_homes
                .OrderBy(x => x.property_type)
                .Skip(0)
                .Take(100)
                .ToList();
        }
这是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; }
}
有人能提出克服这一问题的方法吗

我的直觉是EF正在将表转换为错误的模型,可能是GUI。但是,oldguids=True应该能够克服这一问题


因为它是EF数据库,所以我无法更改生成的类,因为如果更新数据库模型,它将被覆盖。

是的,这不是很明显的原因,因为您的表定义说
id
char
类型吗

`id` char(36) NOT NULL,
但是您的模型定义为
Guid

public System.Guid id { get; set; }
您也应该在模型中将
id
定义为
string

public string id { get; set; }

是的,这不是很明显,因为您的表定义说
id
char
类型吗

`id` char(36) NOT NULL,
但是您的模型定义为
Guid

public System.Guid id { get; set; }
您也应该在模型中将
id
定义为
string

public string id { get; set; }

将Entityframework实体从Guid更改为字符串

将Entityframework实体从Guid更改为字符串

尝试设置oldguid=false。当mysql列的类型为Binary(16)时,似乎使用了true。检查文档

此选项是在Connector/Net 6.1.1中引入的。后端 GUID类型的表示形式已从二进制(16)更改为字符(36)。 这样做是为了允许开发人员使用服务器函数UUID()来 填充GUID表-UUID()生成36个字符的字符串。 较旧应用程序的开发人员可以将“Old Guids=true”添加到 使用数据类型为二进制(16)的GUID的连接字符串


尝试将oldguid设置为false。当mysql列的类型为Binary(16)时,似乎使用了true。检查文档

此选项是在Connector/Net 6.1.1中引入的。后端 GUID类型的表示形式已从二进制(16)更改为字符(36)。 这样做是为了允许开发人员使用服务器函数UUID()来 填充GUID表-UUID()生成36个字符的字符串。 较旧应用程序的开发人员可以将“Old Guids=true”添加到 使用数据类型为二进制(16)的GUID的连接字符串


首先,有一个导致这种行为的
MySql
bug

将文本“old guids=True”添加到连接字符串应该可以解决此问题

如果问题仍然存在,请检查实体框架生成的类的类型是否为
string
,而不是
Guid

public System.Guid id { get; set; }

作为最后一次资源检查,查看EF、mysql连接器或mysql服务器是否有任何更新

首先,有一个导致此行为的
mysql
错误

将文本“old guids=True”添加到连接字符串应该可以解决此问题

如果问题仍然存在,请检查实体框架生成的类的类型是否为
string
,而不是
Guid

public System.Guid id { get; set; }

作为最后一次资源检查,查看EF、mysql连接器或mysql服务器是否有任何更新

我知道这就是问题所在。但是EF生成了模型。根据文档,连接字符串中的oldguids=True应该可以解决这个“问题”@TrevorDaniel,不要认为MySQL支持Guid。。。你需要检查一下out@Rahulmysql连接器就是这样映射的。我知道这就是问题所在。但是EF生成了模型。根据文档,连接字符串中的oldguids=True应该可以解决这个“问题”@TrevorDaniel,不要认为MySQL支持Guid。。。你需要检查一下out@Rahul这就是mysql连接器映射它的方式。这是数据库第一个EF-因此,如果重新生成模型,它将被覆盖这是数据库第一个EF-因此,如果重新生成模型,它将被覆盖值得一试,但这会将错误更改为System.FormatException:Guid应包含32位数字和4个破折号(xxxxxxxx-xxxx-xxxx-xxxx-XXXXXXXXXX)。您能检查表中的所有guid数据是否有效吗?可能会使用reg ex运行查询。它们都是填充的-没有空值-具有类似“1000024”@TrevorDaniel的值。您能将数据插入guid列吗?值得一试,但这会将错误更改为System。FormatException:guid应包含32位数字和4个破折号(xxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxxxxxx-xxxxxxxxxxxx)。您可以检查表中的所有guid数据是否有效吗?可以使用reg ex运行查询。它们都是填充的-没有空值-具有类似“1000024”的值@TrevorDaniel。您可以将数据插入guid列吗?