我有一个表,其列定义为Identity列增加1.我的问题几乎与this one相同...但是,我的代码有点不同。我使用Table-Per-Type(TPT)继承将我的实体框架代码编写为Code First。我在SQL CE数据库中有一个ContractHead表,一个RentalContract表和一个StorageContract表。相应的RentalContract和StorageContract实体派生自ContractHead实体。
[DataContract]
[Table("ContractHead")]
public class Contract
{
[Key]
[DataMember]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int64 Id { get; set; }
[DataMember]
[Column("ContactName")]
[DataType(DataType.Text)]
public String ContactName { get; set; }
[DataMember]
[DataType(DataType.EmailAddress)]
[Column("ContactEmail")]
public String ContactEmail { get; set; }
[DataMember]
[DataType(DataType.Text)]
[Column("ContactAddress1")]
public String ContactAddress1 { get; set; }
//
// ... MORE PROPERTIES
}
[DataContract]
[Table("ContractRental")]
public class RentalContract : Contract
{
[DataMember]
[Column("BoatMake")]
public String BoatMake { get; set; }
[DataMember]
[Column("BoatYear")]
public Int32? BoatYear { get; set; }
[DataMember]
[Column("BoatBeam")]
public Decimal? BoatBeam { get; set; }
[DataMember]
[Column("BoatType")]
[DataType(DataType.Text)]
public String BoatType { get; set; }
//
// ... MORE PROPERTIES
}
[DataContract]
[Table("ContractStorage")]
public class StorageContract : Contract
{
[DataMember]
[Column("BoatMaxWidth")]
public Decimal? BoatMaxWidth { get; set; }
[DataMember]
[ColumnAttribute("WidthUOM")]
[DataType(DataType.Text)]
public String WidthUOM { get; set; }
[DataMember]
[Column("LaunchDate")]
[DataType(DataType.Date)]
public DateTime? LaunchDate { get; set; }
[DataMember]
[Column("TotalCharge")]
public Decimal? TotalCharge { get; set; }
//
// ... MORE PROPERTIES
}
正如您所看到的,我使用DatabaseGenerated属性修饰了主Key属性。事实上,当我第一次添加这个时,为了解决这个问题,我能够成功地添加一条记录...只有一个...之后,错误返回。当我创建一个新的Contract实例时,ID被赋值为0.因为它是一个关键字段,我认为它是1)必需的2)填充了它的默认值,因为我没有明确地设置它。定义新合同后,它将传递给一个方法,该方法实现了添加新实体的基本EF Add()方法......
private PikeMarine.Core.Models.RentalContract MapRentalContract()
{
PikeMarine.Core.Models.RentalContract contract = new Core.Models.RentalContract();
//
// (Code omitted) Fields are assigned values from form fields here ... The ID property is left unassigned
//
AddRentalContract(RentalContract contract);
}
public RentalContract AddRentalContract(RentalContract contract)
{
RentalContract ret = (RentalContract)base.db.Contracts.Add(contract);
base.db.SaveChanges();
return ret;
}
正在调用SaveChanges()时会生成错误。当我尝试插入新记录时,我收到了错误...
"The column cannot be modified [ColumnName = Id]"
我对此感到困惑,因为数据库中的ID字段(ContractHead表)被设置为一个标识列,它自动生成并递增1.不知道EF知道吗?不是那个" DatabaseGenerated"属性告诉框架?为什么要尝试设置该字段的值?
困惑!
答案 0 :(得分:0)
问题是表示衍生对象的表(StorageContract& RentakContract)每个都将ID字段标识为IDENTITY字段,这是一个问题。一旦我重新定义它们,使它们不是IDENTITY列,ADD功能就开始工作了。
<强> BEFORE 强>
CREATE TABLE [ContractHead]
(
[Id] BIGINT NOT NULL IDENTITY (1,1),
-- Field definitions
);
CREATE TABLE [StorageContract]
(
[Id] BIGINT NOT NULL IDENTITY (1,1), -- unique constraint, foreign key to ContractHead.Id
-- Field definitions
);
CREATE TABLE [RentalContract]
(
[Id] BIGINT NOT NULL IDENTITY (1,1), -- unique constraint, foreign key to ContractHead.Id
-- Field definitions
);
<强> AFTER 强>
CREATE TABLE [ContractHead]
(
[Id] BIGINT NOT NULL IDENTITY (1,1),
-- Field definitions
);
CREATE TABLE [StorageContract]
(
[Id] BIGINT NOT NULL, -- unique constraint, foreign key to ContractHead.Id
-- Field definitions
);
CREATE TABLE [RentalContract]
(
[Id] BIGINT NOT NULL, -- unique constraint, foreign key to ContractHead.Id
-- Field definitions
);