为什么 EF 将 char 属性 视为实际 guid?
Why is EF treating a char property as an actual guid?
我实际上拥有的是一个 属性 类型的 char,我用它来存储 guid 的字符串值。
我当前的设置:
- MySQL
- EF 代码优先
- EF 迁移
型号:
public class Employee
{
public string Id { get; set; }
}
EF 配置
public class EmployeeConfiguration : EntityConfiguration<Employee>
{
public EmployeeConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id)
.HasColumnType(“char”)
.HasMaxLength(36)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}
问题:
如果我执行以下操作,则会抛出 System.FormatException:
1.插入不符合guid格式的Id。
2. 从包含不遵循 guid 格式的 Id 的数据库中查询上述 Employee class。
如果我尝试使用 sql(不使用 EF)直接插入或查询数据库,一切都很好。
你能指导我如何解决这个问题吗?
编辑 1:
错误消息
Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
对我有用的解决方案
根据 "Jason Armstrong" 建议,我使用了不同的字符长度,我使用了 char(37)。
EntityFramework 根据 Entity Framework Data Type Mapping
自动将 CHAR(36) 映射到 MySQL 的 GUID
尝试将 old guids=true
添加到每个 C# & MySQL Connector: Guid should contain 32 digits with 4 dashes
的连接字符串中
我实际上拥有的是一个 属性 类型的 char,我用它来存储 guid 的字符串值。
我当前的设置:
- MySQL
- EF 代码优先
- EF 迁移
型号:
public class Employee
{
public string Id { get; set; }
}
EF 配置
public class EmployeeConfiguration : EntityConfiguration<Employee>
{
public EmployeeConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id)
.HasColumnType(“char”)
.HasMaxLength(36)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}
问题:
如果我执行以下操作,则会抛出 System.FormatException:
1.插入不符合guid格式的Id。
2. 从包含不遵循 guid 格式的 Id 的数据库中查询上述 Employee class。
如果我尝试使用 sql(不使用 EF)直接插入或查询数据库,一切都很好。 你能指导我如何解决这个问题吗?
编辑 1:
错误消息
Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
对我有用的解决方案
根据 "Jason Armstrong" 建议,我使用了不同的字符长度,我使用了 char(37)。
EntityFramework 根据 Entity Framework Data Type Mapping
自动将 CHAR(36) 映射到 MySQL 的 GUID尝试将 old guids=true
添加到每个 C# & MySQL Connector: Guid should contain 32 digits with 4 dashes