实体框架代码优先关系和导航属性?

发布于 2024-12-07 16:13:23 字数 1238 浏览 0 评论 0原文

我有以下类,

public class Subject{
        public int SubjectId { get; set; }
        public String SubjectName { get; set; }
        public String SubjectCategory { get; set; }
    }

public class QuestionDescriptor {
        public int QuestionDescriptorId { get; set; }        
        public String QuestionText { get; set; }
        public String Answer { get; set; } 
        public int SubjectId { get; set; }
        public virtual Subject Subject { get; set; }
    }

我使用以下代码配置了它,我希望一个主题可以有许多 QuestionDescriptor

 modelBuilder.Entity<QuestionDescriptor>()
                .HasRequired(qd => qd.Subject)
                .WithMany()
                .HasForeignKey(qd => qd.SubjectId)
                .WillCascadeOnDelete(true);

现在我有以下问题

  1. 我是否正确完成了?
  2. 我需要在主题类中添加导航属性吗?
  3. 如果我这样做会发生什么

    公共类主题 {
            公共 int 主题 ID { 获取;放; }
            公共字符串主题名称{获取;放; }
            公共字符串主题类别{获取;放; }
            公共 int QuestionDescriptorId {获取;设置;}
            公共虚拟 QuestionDescriptor {get;set;} 
        }
    
  4. 如果我执行上述操作,我需要在配置中进行哪些更改,为什么?

  5. 如果我想要属于特定主题的所有问题,那么我可以通过查询 QuestionDescriptor 来获取它们,为什么我需要双向属性?

i have the following classes

public class Subject{
        public int SubjectId { get; set; }
        public String SubjectName { get; set; }
        public String SubjectCategory { get; set; }
    }

public class QuestionDescriptor {
        public int QuestionDescriptorId { get; set; }        
        public String QuestionText { get; set; }
        public String Answer { get; set; } 
        public int SubjectId { get; set; }
        public virtual Subject Subject { get; set; }
    }

i have configured it using the following code ,i want that a Subject can have many QuestionDescriptors

 modelBuilder.Entity<QuestionDescriptor>()
                .HasRequired(qd => qd.Subject)
                .WithMany()
                .HasForeignKey(qd => qd.SubjectId)
                .WillCascadeOnDelete(true);

Now i have the following question

  1. have i done it correctly ?
  2. do i need a navigation property in the Subject class?
  3. what happems if i do this

    public class Subject {
            public int SubjectId { get; set; }
            public String SubjectName { get; set; }
            public String SubjectCategory { get; set; }
            public int QuestionDescriptorId {get;set;}
            public virtual QuestionDescriptor {get;set;} 
        }
    
  4. if i do the above what changes do i need in the configuration and why?

  5. if i want all the questions belonging to a particular subject then i can get them by querying the QuestionDescriptor ,why then do i need a bi-directional property ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独岁月 2024-12-14 16:13:23

1)我做对了吗?

是的。

2) 我需要在主题类中添加导航属性吗?

不,你不需要它。它对于某些查询可能有帮助,但不是必需的。

3)如果我这样做会发生什么......

那是另一种关系。它代表一对一的关系。但是因为您想要一对多关系,所以您的实体上必须有一个导航集合

public class Subject {
    public int SubjectId { get; set; }
    public String SubjectName { get; set; }
    public String SubjectCategory { get; set; }
    public virtual ICollection<QuestionDescriptor> Descriptors {get;set;} 
}

4)如果我执行上述操作,我需要在配置中进行哪些更改以及
为什么?

对于上述更改,您可以保留映射配置不变 - 唯一的例外是您现在必须将集合指定为关系的另一方。您使用的不是 .WithMany()

.WithMany(s => s.Descriptors)

5)如果我想要属于特定主题的所有问题,那么
我可以通过查询 QuestionDescriptor 来获取它们,为什么我需要
双向属性?

你不需要它。

1) have i done it correctly ?

Yes.

2) do i need a navigation property in the Subject class?

No. You don't need it. It can be helpful for certain queries but it is not required.

3) what happems if i do this ...

That's another relationship. It would represent a one-to-one relationship. But because you want a one-to-many relationship you must have a navigation collection on your entity:

public class Subject {
    public int SubjectId { get; set; }
    public String SubjectName { get; set; }
    public String SubjectCategory { get; set; }
    public virtual ICollection<QuestionDescriptor> Descriptors {get;set;} 
}

4) if i do the above what changes do i need in the configuration and
why?

For the change above you can leave you mapping configuration as it is - with the only exception that you now must specify the collection as the other side of the relationship. Instead of .WithMany() you use

.WithMany(s => s.Descriptors)

5) if i want all the questions belonging to a particular subject then
i can get them by querying the QuestionDescriptor ,why then do i need
a bi-directional property ?

You don't need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文