创建外键时出现实体框架错误

时间:2018-10-22 14:23:16

标签: c# entity-framework

我正在尝试使用迁移来生成我的EF数据库,但是在执行update-database命令时遇到了一些错误。

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

错误2

  

如果引用列“ Occupier.OccupierId”是标识列,则无法创建级联外键“ FK_Occupier_Property_OccupierId”。       无法创建约束或索引。查看以前的错误。

从我可以看到的表中正确设置了我的表,但是显然有一些看不见的东西。我将发布下表的代码,希望有人可以看到我所缺少的内容。

为清楚起见; SolicitorInstruction有一个Property对象,其中可能包含许多Occupiers

我也不确定[ForeignKey("string")]的工作方式。这是说“这是主键“ string”的外键吗?还是“ string”是外键?

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

财产

[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>();
    }
}

占领者

[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; }

    }

1 个答案:

答案 0 :(得分:0)

尝试此更改以将导航属性添加到依赖类中。

财产

[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>();
    }
}

占领者

[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;}

    }