0

I have ApplicationForm class to store user's information who apply to the website. ApplicationFormAnswer is for storing custom fields answers in each application form.

 public class ApplicationFormAnswer
    {
        public Guid Id { get; set; }
        public string FieldId { get; set; }
        public ApplicationCustomFieldType FieldType { get; set; }
        public string Name { get; set; }
        public string Answer { get; set; }
        public virtual ApplicationForm ApplicationForm { get; set; }
        public Guid ApplicationFormId { get; set; }
    }

and in ApplicationForm class

public class ApplicationForm
{
    public Guid Id { get; set; }
    .....
    ....
    public virtual ICollection<ApplicationFormAnswer> ApplicationFormAnswers { get; set; }
}

When I try to make a migration it s giving me this error:

There are no primary or candidate keys in the referenced table 'dbo.ApplicationForms' that match the referencing column list in the foreign key 'FK_dbo.ApplicationFormAnswers_dbo.ApplicationForms_ApplicationFormId'. Could not create constraint or index. See previous errors.

EDIT:

when I apply -verbose I see that sql query creating:

CREATE TABLE [dbo].[ApplicationFormAnswers] (
    [ApplicationFormId] [uniqueidentifier] NOT NULL,
    [Id] [uniqueidentifier] NOT NULL,
    [FieldId] [nvarchar](max),
    [FieldType] [int] NOT NULL,
    [Name] [nvarchar](max),
    [Answer] [nvarchar](max),
    [ApplicationFormCustomField_FieldId] [nvarchar](max),
    [ApplicationFormCustomField_Order] [int] NOT NULL,
    [ApplicationFormCustomField_Name] [nvarchar](max),
    [ApplicationFormCustomField_FieldType] [int] NOT NULL,
    [ApplicationFormCustomField_IsRequired] [bit] NOT NULL,
    [ApplicationFormCustomField_IsIncluded] [bit] NOT NULL,
    [ApplicationFormCustomField_CanBeDeleted] [bit] NOT NULL,
    [ApplicationFormCustomField_IsDeleted] [bit] NOT NULL,
    CONSTRAINT [PK_dbo.ApplicationFormAnswers] PRIMARY KEY ([ApplicationFormId])
)
CREATE INDEX [IX_Id] ON [dbo].[ApplicationFormAnswers]([Id])
ALTER TABLE [dbo].[ApplicationFormAnswers] ADD CONSTRAINT [FK_dbo.ApplicationFormAnswers_dbo.ApplicationForms_Id] FOREIGN KEY ([Id]) REFERENCES [dbo].[ApplicationForms] ([Id]) ON DELETE CASCADE

I see that it making ApplicationFormId as primary key but primary key should be Id. I tried to put [Key] above Id but it still didn't work.

Sefe
  • 13,731
  • 5
  • 42
  • 55
bthn
  • 176
  • 2
  • 13
  • you need to add [Key] in your class above on your property ApplicationFormId – PixelDev Jul 17 '18 at 10:31
  • @PixelDev I tried it but giving same error – bthn Jul 17 '18 at 11:09
  • 1
    add attribute `[Key]` on both `ApplicationForm.Id` and `ApplicationFormAnswer.ApplicationFormId` and still if there is error post you `modelbuilder` from `DbSetModel` for `ApplicationForm` – Sumit raj Jul 17 '18 at 11:14
  • @bthn please check this link https://stackoverflow.com/questions/17879735/there-are-no-primary-or-candidate-keys-in-the-referenced-table-that-match-the-re – PixelDev Jul 17 '18 at 11:54

2 Answers2

0

I think the problem is that you are setting [ApplicationFormId] as a unique identfier when it's only a FK.

Just use int type in ApplicationFormAnswers for [ApplicationFormId]

Gonzalo Lorieto
  • 635
  • 6
  • 24
0

Since your Id property is a GUID and not an int, EF will not automatically recognize it as an ID. You can either use the [KeyAttribute] or the fluent API to manually configure the ID:

public class ApplicationFormAnswer
{
     [Key]
     public Guid Id { get; set; }
     //...
}

public class ApplicationForm
{
     [Key]
     public Guid Id { get; set; }
     //...
}

...or...

modelBuilder.Entity<ApplicationFormAnswer>().HasKey(e => e.Id);

You should also manually configure the foreign keys:

public class ApplicationFormAnswer
{
     [Key]
     public Guid Id { get; set; }
     public Guid ApplicationFormId { get; set; }
     [ForeignKey("ApplicationFormId")]         
     public virtual ApplicationForm ApplicationForm { get; set; }
     //...
}

...or...

modelBuilder.Entity<ApplicationFormAnswer>()
    .HasOne(e => e.ApplicationForm)
    .WithMany(e => e.ApplicationFormAnswers)
    .HasForeignKey(e => e.ApplicationFormId);
Sefe
  • 13,731
  • 5
  • 42
  • 55