I am using Entity Framework Code-First and I am new to that. Here are my models:
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; }
}
When i add ( add-migration personnel_1
-context
PersonnelDbContext
) it gives me below error :
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.
That does not look like a correct model ( fld_organization_type_id_ref
is not shown or is mistyped). IAC, that field needs to be of the same type as the primary key for Tbl_Organization_Type (byte). Try:
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; }
}
Similarly, you need to show/define the field fld_location_id_ref
and match the type with the reference table Tbl_Personnel_Location.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.