我有一个包含以下列的表格:
ContractorId .... INT .... IDENTITY
ContractorName .... Varchar(50) .... PK
ContractorGrade ... .... Varchar(3) .... PK
PetaPoco T4 模板生成的类如下所示:
[TableName("contractor_master")]
[PrimaryKey("contractorname", autoIncrement=false)]
[ExplicitColumns]
public partial class contractor_master : TubewellRepo.Record<contractor_master>
{
[Column]
public int contractorid
{
get
{
return _contractorid;
}
set
{
_contractorid = value;
MarkColumnModified("contractorid");
}
}
int _contractorid;
[Column]
public string contractorname
{
get
{
return _contractorname;
}
set
{
_contractorname = value;
MarkColumnModified("contractorname");
}
}
string _contractorname;
[Column]
public string contractorgrade
{
get
{
return _contractorgrade;
}
set
{
_contractorgrade = value;
MarkColumnModified("contractorgrade");
}
}
string _contractorgrade;
}
插入新记录的代码如下:
// Insert a record
var Contractor=new contractor_master();
Contractor.contractorname = "Super Borewells";
Contractor.contractorgrade = "A";
db.Insert(Contractor);
在 Class Code 的第二行,我想知道如何提及一个 Composite Key,即 (ContractorName + ContractorGrade)。
其次,它没有插入记录,因为它需要一个 Id 列。即使 ContractorId 是 IDENTITY,它也不是主键。
它没有插入新记录并给出错误,因为它在 IDENTITY 列中插入 0。