polonskyg
polonskyg

Reputation: 4319

EF5 error: Cascading foreign key FK_... where the referencing column X is an identity column. Could not create constraint

I'm working on an Entity Framework 5 Code First project. Right now I'm gettcannot be createding the error:

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

These are my entities, Why I'm getting that error? I'm not saying PersonId is identity there in DriverLicense entity.

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

        }

Thanks in advance! Guillermo.

EDIT: With SQL Profiler I can see this:

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

I do not understand why PersonId is created as Identity

Upvotes: 1

Views: 1418

Answers (2)

Mohammad Jawad Barati
Mohammad Jawad Barati

Reputation: 756

i have the same problem:

you must change this public long PersonId { get; set; } property to public long? PersonId { get; set; }

because when you try to add for first time, a new DriverLicense you can't assign DriverLicense.PersonId to any object (because you don't have any Person for assign) so you must set PersonId property to nullable.

edit: this link can help you

Upvotes: 0

polonskyg
polonskyg

Reputation: 4319

Ok, I've found the problem. I'm using Migrations, and there was one pending which had the wrong model. So, if you're in the same situation, check your pending migrations! :) Thanks! Guillermo.

Upvotes: 1

Related Questions