c# - ef代码第一个principal和dependent的区别

标签 c# entity-framework ef-code-first

我有这些实体:

 public class StudentBag
 {
    public int BagIdentifier { get; set; }
    public Student Student { get; set; }
 }

 public class Student
 {
    public string Name { get; set; }
    public StudentBag StudentBag{get;set;}

 }

我想配置一个一对一的关系。我的问题是:

modelBuilder.Entity<StudentBag>()
            .HasRequired(t => t.Student)
            .WithRequiredDependent(t=>t.StudentBag);

        modelBuilder.Entity<StudentBag>()
            .HasRequired(t => t.Student)
            .WithRequiredPrincipal(t => t.StudentBag);

如果有人能解释一下原则和依赖的含义,我将不胜感激......

最佳答案

很简单,对外部类有导航属性的类就是Principal。

需要的类有对主类的引用时,你应该使用: WithRequiredPrincipal()

需要的类被主类引用时,你应该使用: WithRequiredDependent()

例如。这样下面的两张 map 是一样的。

    //modelBuilder.Entity<>(Student)
        //.HasRequired(t => t.StudentBag)
        //.WithRequiredDependent();

    modelBuilder.Entity<Student>()
        .HasRequired(t => t.StudentBag)
        .WithRequiredDependent();

    modelBuilder.Entity<StudentBag>()
        .HasRequired(t => t.Student)
        .WithRequiredPrincipal();

关于c# - ef代码第一个principal和dependent的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24053263/

相关文章:

c# - 将可见性绑定(bind)到可检查的菜单项在 WPF 中显示错误 "Service provider is missing the INameResolver service"

c# - 在 C# 的泛型类中获取嵌套泛型类的类型

entity-framework - EF 6、代码第一联结表名称

entity-framework - 您如何确保首先对 EF 代码中的表关系启用级联删除?

c# - GetBytes 函数如何工作?

c# - 如何从 .NET 中的 x 行读取文件

c# - 将 Entity Framework Core 示例转换为 F#

c# - Entity Framework - 循环遍历属性以进行更新

c# - Entity Framework 快照历史

c# - 在一个 DBContext 中处理多个模式