我有以下数据库表:
CREATE TABLE [dbo].[QUESTIONS]( [QUESTION_ID] [int] IDENTITY(1,1) NOT NULL, [QUESTION_TEXT] [varchar](max) NOT NULL, [PARENT_QUESTION_ID] [int] NULL, CONSTRAINT [PK_QUESTIONS] PRIMARY KEY CLUSTERED ( [QUESTION_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] TEXTIMAGE_ON [PRIMARY] GO ALTER TABLE [dbo].[QUESTIONS] WITH CHECK ADD CONSTRAINT [FK_QUESTIONS_QUESTIONS] FOREIGN KEY([PARENT_QUESTION_ID]) REFERENCES [dbo].[QUESTIONS] ([QUESTION_ID]) GO ALTER TABLE [dbo].[QUESTIONS] CHECK CONSTRAINT [FK_QUESTIONS_QUESTIONS] GO基本上PARENT_QUESTION_ID是QUESTION_ID上的自联接。 我试图在EF 6.0 Code First中实现这一点。
在课堂上我有:
[Serializable] [Table("QUESTIONS")] public class Question : BaseDomainModel, IQuestion { [Column("QUESTION_ID")] public override int? Id { get; set; } [Column("QUESTION_TEXT")] [Required] public string QuestionText { get; set; } [Column("PARENT_QUESTION_ID")] public int? ParentQuestionId { get; set; } public virtual IQuestion Parent { get; set; } [ForeignKey("ParentQuestionId")] public virtual List<IQuestion> ChildQuestions { get; set; } public Question() { ChildQuestions = new List<IQuestion>(); } }代码执行但ChildQuestions永远不会填充。 例如,如果我在数据库中有这个:
记录24和25应该填写在问题26的ChildQuestions列表中。相反,没有任何内容出现。
这是我从存储库填充问题的调用:
public IQuestion GetById(int id) { IQuestion question = null; using (var db = new MainContext()) { question = db.Questions.SingleOrDefault(a => a.Id == id); } return question; }这是我正在使用的上下文:
internal class MainContext : DbContext { internal MainContext() : base("name=ACME.Context") { } public virtual DbSet<Question> Questions { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer<MainContext>(null); base.OnModelCreating(modelBuilder); } }I have the following database table:
CREATE TABLE [dbo].[QUESTIONS]( [QUESTION_ID] [int] IDENTITY(1,1) NOT NULL, [QUESTION_TEXT] [varchar](max) NOT NULL, [PARENT_QUESTION_ID] [int] NULL, CONSTRAINT [PK_QUESTIONS] PRIMARY KEY CLUSTERED ( [QUESTION_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] TEXTIMAGE_ON [PRIMARY] GO ALTER TABLE [dbo].[QUESTIONS] WITH CHECK ADD CONSTRAINT [FK_QUESTIONS_QUESTIONS] FOREIGN KEY([PARENT_QUESTION_ID]) REFERENCES [dbo].[QUESTIONS] ([QUESTION_ID]) GO ALTER TABLE [dbo].[QUESTIONS] CHECK CONSTRAINT [FK_QUESTIONS_QUESTIONS] GOBasically PARENT_QUESTION_ID is a self join on QUESTION_ID. I'm trying to implement this in EF 6.0 Code First.
In the class I have:
[Serializable] [Table("QUESTIONS")] public class Question : BaseDomainModel, IQuestion { [Column("QUESTION_ID")] public override int? Id { get; set; } [Column("QUESTION_TEXT")] [Required] public string QuestionText { get; set; } [Column("PARENT_QUESTION_ID")] public int? ParentQuestionId { get; set; } public virtual IQuestion Parent { get; set; } [ForeignKey("ParentQuestionId")] public virtual List<IQuestion> ChildQuestions { get; set; } public Question() { ChildQuestions = new List<IQuestion>(); } }The code executes but ChildQuestions never populates. For example, if I have this in the database:
Records 24 and 25 should be populated in ChildQuestions list for Question 26. Instead nothing appears.
Here is the call I make to populate the Question from the Repository:
public IQuestion GetById(int id) { IQuestion question = null; using (var db = new MainContext()) { question = db.Questions.SingleOrDefault(a => a.Id == id); } return question; }Here is the context that I'm using:
internal class MainContext : DbContext { internal MainContext() : base("name=ACME.Context") { } public virtual DbSet<Question> Questions { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer<MainContext>(null); base.OnModelCreating(modelBuilder); } }最满意答案
你在错误的属性上有ForeignKey属性。 不是在ChildQuestions它应该在Parent :
[ForeignKey("ParentQuestionId")] public virtual IQuestion Parent { get; set; } public virtual List<IQuestion> ChildQuestions { get; set; }You've got the ForeignKey attribute on the wrong property. Instead of being on ChildQuestions it should be on Parent:
[ForeignKey("ParentQuestionId")] public virtual IQuestion Parent { get; set; } public virtual List<IQuestion> ChildQuestions { get; set; }更多推荐
发布评论