实体框架6自我加入不填充(Entity Framework 6 Self Join not populating)

编程入门 行业动态 更新时间:2024-10-10 08:19:23
实体框架6自我加入不填充(Entity Framework 6 Self Join not populating)

我有以下数据库表:

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] GO

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

更多推荐

本文发布于:2023-07-04 09:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1022361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   自我   Entity   Join

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!