EF代码优先原则和从属原则之间的区别 [英] Ef code first difference between principle and dependent

查看:108
本文介绍了EF代码优先原则和从属原则之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下实体:

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

 }



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

I want to configure a one to one relationship. my question is if there is a difference between:

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

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

我将不胜感激,如果有人能解释这是什么原则和依赖...

and I will appreciate if someone will explain what it is mean principle and dependent...

推荐答案

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

It's pretty simple, the class that has the navigation property to the foreign class is the Principal.

必需的类具有对主类的引用时,您应该使用:
WithRequiredPrincipal()

当主类引用了必需的类时,您应该使用:
WithRequiredDependent()

When the required class is referenced by the main class you should use: WithRequiredDependent()

例如。这样,下面的两个映射是相同的。

Eg. That way the two maps below are the same.

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

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

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

这篇关于EF代码优先原则和从属原则之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆