ef-code-first 与 tinyint 主键迁移的一对多关系失败
ef-code-first one-to-many relation with tinyint primary key migration fails
我正在使用 Entity Framework Code-First,我是新手。这是我的模型:
public class Tbl_Organization_Type
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public byte fld_organization_type_id { get; set; }
[MaxLength(100)]
public string fld_organization_type_name { get; set; }
public byte? fld_sort { get; set; }
public ICollection<Tbl_Organization> Tbl_Organization { get; set; }
}
public class Tbl_Organization
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long fld_organization_id { get; set; }
public long? fld_organization_parent_id_ref { get; set; }
[StringLength(500)]
public string fld_organization_name { get; set; }
[StringLength(200)]
public string fld_organization_address { get; set; }
[ForeignKey("fld_location_id_ref")]
public Tbl_Personnel_Location Tbl_Personnel_Location { get; set; }
[ForeignKey("fld_organization_type_id_ref")]
public Tbl_Organization_Type Tbl_Organization_Type { get; set; }
}
当我添加 ( add-migration personnel_1
-context
PersonnelDbContext
)
它给了我以下错误:
The relationship from 'Tbl_Organization.fld_organization_type_id' to
'Tbl_Organization_Type.Tbl_Organization' with foreign key properties
{'fld_organization_type_id_ref' : Nullable} cannot target the
primary key {'fld_organization_type_id' : byte} because it is not
compatible.
Configure a principal key or a set of compatible foreign
key properties for this relationship.
这看起来不像是正确的模型(fld_organization_type_id_ref
未显示或输入错误)。 IAC,该字段需要与 Tbl_Organization_Type (字节)的主键类型相同。尝试:
public class Tbl_Organization
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long fld_organization_id { get; set; }
public byte? fld_organization_type_id_ref { get; set; } // This FK needs to match referenced PK type
// Are you missing or not showing fld_location_id_ref
[StringLength(500)]
public string fld_organization_name { get; set; }
[StringLength(200)]
public string fld_organization_address { get; set; }
[ForeignKey("fld_location_id_ref")]
public Tbl_Personnel_Location Tbl_Personnel_Location { get; set; }
[ForeignKey("fld_organization_type_id_ref")]
public Tbl_Organization_Type Tbl_Organization_Type { get; set; }
}
同样,您需要 show/define 字段 fld_location_id_ref
并将类型与引用匹配 table Tbl_Personnel_Location.
我正在使用 Entity Framework Code-First,我是新手。这是我的模型:
public class Tbl_Organization_Type
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public byte fld_organization_type_id { get; set; }
[MaxLength(100)]
public string fld_organization_type_name { get; set; }
public byte? fld_sort { get; set; }
public ICollection<Tbl_Organization> Tbl_Organization { get; set; }
}
public class Tbl_Organization
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long fld_organization_id { get; set; }
public long? fld_organization_parent_id_ref { get; set; }
[StringLength(500)]
public string fld_organization_name { get; set; }
[StringLength(200)]
public string fld_organization_address { get; set; }
[ForeignKey("fld_location_id_ref")]
public Tbl_Personnel_Location Tbl_Personnel_Location { get; set; }
[ForeignKey("fld_organization_type_id_ref")]
public Tbl_Organization_Type Tbl_Organization_Type { get; set; }
}
当我添加 ( add-migration personnel_1
-context
PersonnelDbContext
)
它给了我以下错误:
The relationship from 'Tbl_Organization.fld_organization_type_id' to 'Tbl_Organization_Type.Tbl_Organization' with foreign key properties {'fld_organization_type_id_ref' : Nullable} cannot target the primary key {'fld_organization_type_id' : byte} because it is not compatible.
Configure a principal key or a set of compatible foreign key properties for this relationship.
这看起来不像是正确的模型(fld_organization_type_id_ref
未显示或输入错误)。 IAC,该字段需要与 Tbl_Organization_Type (字节)的主键类型相同。尝试:
public class Tbl_Organization
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long fld_organization_id { get; set; }
public byte? fld_organization_type_id_ref { get; set; } // This FK needs to match referenced PK type
// Are you missing or not showing fld_location_id_ref
[StringLength(500)]
public string fld_organization_name { get; set; }
[StringLength(200)]
public string fld_organization_address { get; set; }
[ForeignKey("fld_location_id_ref")]
public Tbl_Personnel_Location Tbl_Personnel_Location { get; set; }
[ForeignKey("fld_organization_type_id_ref")]
public Tbl_Organization_Type Tbl_Organization_Type { get; set; }
}
同样,您需要 show/define 字段 fld_location_id_ref
并将类型与引用匹配 table Tbl_Personnel_Location.