简体   繁体   中英

Entity Framework error when creating foreign keys

I'm attempting to use migrations to generate my EF database, but I'm getting a couple of errors when I'm executing the update-database command.

Error 1

Failed executing DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Occupier] (
    [OccupierId] int NOT NULL IDENTITY,
    [Title] int NOT NULL,
    [FirstName] nvarchar(32) NULL,
    [LastName] nvarchar(32) NULL,
    [Dob] datetime2 NOT NULL,
    [Relationship] nvarchar(32) NULL,
    CONSTRAINT [PK_Occupier] PRIMARY KEY ([OccupierId]),
    CONSTRAINT [FK_Occupier_Property_OccupierId] FOREIGN KEY ([OccupierId]) REFERENCES [Property] ([PropertyId]) ON DELETE CASCADE
);

Error 2

Cascading foreign key 'FK_Occupier_Property_OccupierId' cannot be created where the referencing column 'Occupier.OccupierId' is an identity column. Could not create constraint or index. See previous errors.

From what I can see I have my tables set up correctly, but there's obviously something missing which I can't see. I'll post the code for the tables below and hopefully someone can see what I'm missing.

For clarity; a SolicitorInstruction has a Property object, which may contain a number of Occupiers .

I'm also not sure about the way [ForeignKey("string")] works. Is this saying "this is a foreign key to primary key "string"? or "string" is a foreign key?

SolicitorInstruction

public class SolicitorInstruction
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int SolicitorInstructionId { get; set; }
        public InstructionTypes InstructionType { get; set; }
        public int ApplicationId { get; set; }
        public DateTime DateLogged { get; set; }
        [ForeignKey("AdditionalInformationId")]
        public AdditionalInformation AdditionalInformation { get; set; }
        [ForeignKey("BorrowerBankId")]
        public BorrowerBank BorrowerBank { get; set; }
        [ForeignKey("BrokerId")]
        public Broker Broker { get; set; }
        [ForeignKey("PropertyId")]
        public Property Property { get; set; }
        [ForeignKey("SolicitorId")]
        public Solicitor Solicitor { get; set; }
        [ForeignKey("BorrowerId")]
        public List<Borrower> Borrower { get; set; }
        [ForeignKey("CurrentLenderId")]
        public List<CurrentLender> CurrentLender { get; set; }
    }

Property

[Table("Property")]
public partial class Property
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int PropertyId { get; set; }

    [StringLength(8)]
    public string ReferenceNumber { get; set; }
    [StringLength(4)]
    public string CaseOwner { get; set; }
    public int AmountBorrowed { get; set; }
    [ForeignKey("SecurityAddressId")]
    public Address Security { get; set; }
    [ForeignKey("CorrespondenceAddressId")]
    public Address Correspondence { get; set; }
    [ForeignKey("OccupierId")]
    public List<Occupier> Occupier { get; set; }
    public TenureTypes Tenure { get; set; }
    public JurisdictionTypes Jurisdiction { get; set; }
    public FunderTypes Funder { get; set; }

    public Property()
    {
        Occupier = new List<Occupier>();
    }
}

Occupier

[Table("Occupier")]
    public partial class Occupier
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int OccupierId { get; set; }
        public Honorifics Title { get; set; }
        [StringLength(32)]
        public string FirstName { get; set; }
        [StringLength(32)]
        public string LastName { get; set; }
        public DateTime Dob { get; set; }
        [StringLength(32)]
        public string Relationship { get; set; }

    }

Try this changes to add the navigation property in the dependent class.

Property

[Table("Property")]
public partial class Property
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int PropertyId { get; set; }

    [StringLength(8)]
    public string ReferenceNumber { get; set; }
    [StringLength(4)]
    public string CaseOwner { get; set; }
    public int AmountBorrowed { get; set; }
    [ForeignKey("SecurityAddressId")]
    public Address Security { get; set; }
    [ForeignKey("CorrespondenceAddressId")]
    public Address Correspondence { get; set; }
    public List<Occupier> Occupier { get; set; }
    public TenureTypes Tenure { get; set; }
    public JurisdictionTypes Jurisdiction { get; set; }
    public FunderTypes Funder { get; set; }

    public Property()
    {
        Occupier = new List<Occupier>();
    }
}

Occupier

[Table("Occupier")]
    public partial class Occupier
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int OccupierId { get; set; }
        public Honorifics Title { get; set; }
        [StringLength(32)]
        public string FirstName { get; set; }
        [StringLength(32)]
        public string LastName { get; set; }
        public DateTime Dob { get; set; }
        [StringLength(32)]
        public string Relationship { get; set; }
        [Required]
        public int PropertyId { get; set; }
        [ForeignKey(PropertyId)]
        public Property Property {get; set;}

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM