1

私は Entity Framework 5 Code First プロジェクトに取り組んでいます。現在、エラーを作成できません:

"Cascading foreign key 'FK_dbo.DriverLicense_dbo.Person_PersonId' where the referencing column 'DriverLicense.PersonId' is an identity column.\r\nCould not create constraint

これらは私のエンティティですが、なぜそのエラーが発生するのですか? PersonId が DriverLicense エンティティのアイデンティティであると言っているのではありません。

public class DriverLicense
{
    public long DriverLicenseId { get; set; }
    public long LicenseNumber { get; set; }
    public long PersonId { get; set; }
    public virtual Person Person { get; set; }
}

public class DriverLicenseMap : EntityTypeConfiguration<DriverLicense>
{
        public DriverLicenseMap()
        {
            this.HasKey(t => t.DriverLicenseId);

            // Table & Column Mappings
            this.ToTable("DriverLicense");
            this.Property(t => t.DriverLicenseId).HasColumnName("DriverLicenseId");
            this.Property(t => t.LicenseNumber).HasColumnName("LicenseNumber");
            this.Property(t => t.PersonId).HasColumnName("PersonId");

            this.HasRequired(t => t.Person)
                .WithMany(t => t.DriverLicenses)
                .HasForeignKey(d => d.PersonId);

        }
}

public class Person
{
    public Person()
    {
        this.DriverLicenses = new List<DriverLicense>();
    }

    public long PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public virtual ICollection<DriverLicense> DriverLicenses { get; set; }
}

 public class PersonMap : EntityTypeConfiguration<Person>
    {
        public PersonMap()
        {
            // Primary Key
            this.HasKey(t => t.PersonId);

            // Properties
            this.Property(t => t.FirstName)
                .IsRequired()
                .HasMaxLength(50);

            this.Property(t => t.LastName)
                .IsRequired()
                .HasMaxLength(50);

            // Table & Column Mappings
            this.ToTable("Person");
            this.Property(t => t.PersonId).HasColumnName("PersonId");
            this.Property(t => t.FirstName).HasColumnName("FirstName");
            this.Property(t => t.LastName).HasColumnName("LastName");

        }

前もって感謝します!ギレルモ。

編集: SQL プロファイラーを使用すると、次のことがわかります。

CREATE TABLE [dbo].[DriverLicense] (
    [DriverLicenseId] [bigint] NOT NULL,
    [LicenseNumber] [bigint] NOT NULL,
    [PersonId] [bigint] NOT NULL IDENTITY,
    CONSTRAINT [PK_dbo.DriverLicense] PRIMARY KEY ([DriverLicenseId])
)

PersonId が Identity として作成される理由がわかりません

4

2 に答える 2

1

わかりました、問題が見つかりました。移行を使用していますが、保留中のモデルが間違っていました。したがって、同じ状況にある場合は、保留中の移行を確認してください。:) ありがとう!ギレルモ。

于 2012-12-26T22:43:30.377 に答える