0

我有一个表,其列定义为以 1 递增的标识列。我的问题几乎与这个相同......但是,我的代码有点不同。我正在使用 Table-Per-Type (TPT) 继承将我的实体框架代码编写为代码优先。我在 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 属性修饰了主键属性。事实上,当我第一次添加这个时,为了解决这个问题,我成功地添加了一条记录......只有一条......之后,错误返回了。当我创建一个 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]"

我对此感到困惑,因为数据库(ContractHead 表)中的 ID 字段设置为自动生成并递增 1 的标识列。EF 不知道这一点吗?这不是“DatabaseGenerated”属性告诉框架的吗?为什么要尝试设置该字段的值?

使困惑!

4

1 回答 1

0

问题是表示派生对象(StorageContract 和 RentakContract)的表每个都将 ID 字段标识为 IDENTITY 字段,这是一个问题。一旦我重新定义了它们,它们就不是 IDENTITY 列,ADD 函数就开始工作了。

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
);

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
);
于 2013-09-15T02:06:51.593 回答