实体框架使用不正确的列名生成SQL

时间:2015-06-30 18:22:46

标签: entity-framework entity

我有几个表问题和QuestionQueryParameters我试图用实体框架设置。基本上我想知道哪些问题依赖于这个问题以及这个问题所依赖的问题。

根据情况,我收到了无效的列错误" Question_Id1"我假设我没有正确设置外键关系,但我是实体框架工作的新手,并没有"摆弄"已经工作了。

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

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

2 个答案:

答案 0 :(得分:2)

实体框架无法分辨哪个外键匹配,因此它自己创建。解决方案是您拥有的InverseProperty,但您需要指向外键,而不是导航属性:

[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

答案 1 :(得分:1)

如果要在数据库中重命名FK列而不在实体中声明它们,则应使用Fluent API代替Data Annotations来配置这些关系。要更改名称,您需要使用Map方法:

 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

由于您在Question实体中使用复合PK,因此必须按{{1}中主键的相同顺序指定MapKey方法中FK列的名称。实体。

相关问题