Reputation: 11
I have a couple of tables Questions and QuestionQueryParameters I am trying to setup with the entity framework. Essentially I want to see what questions rely on this one and what questions does this question rely on.
As things stand I get an invalid column error "Question_Id1" I am assuming I have not set the foreign key relationships up correctly but I am new to the entity frame work and no amount of "fiddling" has worked yet.
Tables
CREATE TABLE [dbo].[Questions](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Text] [nvarchar](max) NULL,
[Section] [nvarchar](max) NULL,
[Order] [int] NOT NULL,
[Active] [bit] NOT NULL,
[Mandatory] [bit] NOT NULL,
[QuestionType_Id] [int] NULL,
[EntityType_Id] [int] NOT NULL,
[ImportTimestamp] [datetime] NULL,
[ImportUser_Id] [int] NULL,
[Question_Id] [int] NULL,
[DisplayMenuType] [bit] NULL,
[Abbrev] [nvarchar](max) NULL,
[Searchable] [bit] NULL,
CONSTRAINT [PK_Questions] PRIMARY KEY CLUSTERED
(
[Id] ASC,
[EntityType_Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Questions] WITH CHECK ADD CONSTRAINT [FK_Questions_EntityType] FOREIGN KEY([EntityType_Id])
REFERENCES [dbo].[EntityType] ([Id])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Questions] CHECK CONSTRAINT [FK_Questions_EntityType]
GO
ALTER TABLE [dbo].[Questions] WITH CHECK ADD CONSTRAINT [FK_Questions_QuestionTypes] FOREIGN KEY([QuestionType_Id])
REFERENCES [dbo].[QuestionTypes] ([Id])
GO
ALTER TABLE [dbo].[Questions] CHECK CONSTRAINT [FK_Questions_QuestionTypes]
GO
ALTER TABLE [dbo].[Questions] WITH CHECK ADD CONSTRAINT [FK_Questions_User] FOREIGN KEY([ImportUser_Id])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[Questions] CHECK CONSTRAINT [FK_Questions_User]
GO
CREATE TABLE [dbo].[QuestionQueryParameters](
[QuestionQueryParameter_Id] [int] IDENTITY(1,1) NOT NULL,
[Question_Id] [int] NOT NULL,
[EnablingQuestion_Id] [int] NULL,
[EntityType_Id] [int] NOT NULL,
CONSTRAINT [PK__Question__2FBBD18F3E723F9C] PRIMARY KEY CLUSTERED
(
[QuestionQueryParameter_Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[QuestionQueryParameters] WITH CHECK ADD CONSTRAINT [FK_QuestQueryParam_Questions] FOREIGN KEY([Question_Id], [EntityType_Id])
REFERENCES [dbo].[Questions] ([Id], [EntityType_Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[QuestionQueryParameters] CHECK CONSTRAINT [FK_QuestQueryParam_Questions]
GO
ALTER TABLE [dbo].[QuestionQueryParameters] WITH CHECK ADD CONSTRAINT [FK_QuestQueryParam_QuestionsEnabling] FOREIGN KEY([EnablingQuestion_Id], [EntityType_Id])
REFERENCES [dbo].[Questions] ([Id], [EntityType_Id])
GO
ALTER TABLE [dbo].[QuestionQueryParameters] CHECK CONSTRAINT [FK_QuestQueryParam_QuestionsEnabling]
GO
Classes
public class Question
{
[Key, Column(Order = 0)]
public int Id { get; set; }
public string Text { get; set; }
public string Section { get; set; }
public int Order { get; set; }
public bool Active { get; set; }
public bool Mandatory { get; set; }
public bool Searchable { get; set; }
public virtual QuestionType QuestionType { get; set; }
// public virtual TemplateType TemplateType { get; set; }
public virtual List<QuestionChoice> QuestionChoices { get; set; }
public virtual List<QuestionEnabler> QuestionEnablers { get; set; }
public virtual List<QuestionChoiceQuery> QuestionChoicesQuery { get; set; }
[Key, Column(Order = 1)]
public virtual EntityType EntityType { get; set; }
public DateTime? ImportTimestamp { get; set; }
public int? ImportUser_Id { get; set; }
public virtual List<Question> EnabledQuestions { get; set; }
public virtual List<QuestionQueryParameter> QuestionParameters { get; set; }
public bool DisplayMenuType { get; set; }
public string Abbrev { get; set; }
[InverseProperty("Question")]
public virtual ICollection<QuestionQueryParameter> ReliesOnMe { get; set; }
[InverseProperty("EnablingQuestion")]
public virtual ICollection<QuestionQueryParameter> IRelyOn { get; set; }
}
public class QuestionQueryParameter
{
[Key]
public int QuestionQueryParameter_Id { get; set; }
public virtual Question Question { get; set; }
public virtual Question EnablingQuestion { get; set; }
public virtual EntityType EntityType { get; set; }
}
Upvotes: 1
Views: 1038
Reputation: 39326
If you want to rename the FK columns in your DB without declaring them in your entity, you should use Fluent API instead Data Annotations to configure those relationships. To change the names you need to use the Map method:
modelBuilder.Entity<QuestionQueryParameter>()
.HasRequired(s => s.Question)
.WithMany(s => s.ReliesOnMe).Map(ca=>ca.MapKey("Question_Id","EntityType_Id")); // Change the FK column names here
modelBuilder.Entity<QuestionQueryParameter>()
.HasRequired(s => s.EnablingQuestion)
.WithMany(s => s.IRelyOn).Map(ca=>ca.MapKey("EnablingQuestion_Id","EntityType_Id")); // Change the FK column names here
Due to you are using a composite PK in the Question
entity, the name of the FK columns in the MapKey
method must be specified in the same order that the primary keys in the Question
entity.
Upvotes: 1
Reputation: 12304
Entity framework can't tell which foreign key to match up so it makes its own. The solution is InverseProperty which you have, but you need to point to the foreign key, not the navigation property:
[InverseProperty("Question_Id")]
public virtual ICollection<QuestionQueryParameter> ReliesOnMe { get; set; }
[InverseProperty("EnablingQuestion_Id")]
public virtual ICollection<QuestionQueryParameter> IRelyOn { get; set; }
https://msdn.microsoft.com/en-us/data/jj591583.aspx#Relationships
Upvotes: 2