[英]EntityFramework - There are no primary or candidate keys in the referenced table
I have ApplicationForm class to store user's information who apply to the website. 我有ApplicationForm类来存储适用于该网站的用户信息。 ApplicationFormAnswer is for storing custom fields answers in each application form.
ApplicationFormAnswer用于在每个申请表中存储自定义字段答案。
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 并在ApplicationForm类中
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'.
在引用表'dbo.ApplicationForms'中没有与外键'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: 当我应用-verbose时,我看到该sql查询正在创建:
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. 我看到它使ApplicationFormId成为主键,但主键应为ID。 I tried to put [Key] above Id but it still didn't work.
我尝试将[Key]放在ID上方,但仍然无法正常工作。
I think the problem is that you are setting [ApplicationFormId]
as a unique identfier when it's only a FK. 我认为问题是,当您仅将
[ApplicationFormId]
设置为FK时,会将其设置为唯一标识符。
Just use int
type in ApplicationFormAnswers
for [ApplicationFormId]
只需在
[ApplicationFormId]
ApplicationFormAnswers
使用int
类型
Since your Id
property is a GUID and not an int
, EF will not automatically recognize it as an ID. 由于您的
Id
属性是GUID而不是int
,因此EF不会自动将其识别为ID。 You can either use the [KeyAttribute]
or the fluent API to manually configure the ID: 您可以使用
[KeyAttribute]
或fluent API手动配置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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.