zoukankan      html  css  js  c++  java
  • 使用Fluent配置表关系

    转载MS官方文档:https://msdn.microsoft.com/zh-cn/data/jj591620

    Configuring Relationships with the Fluent API

    在EFCodeFirst Entity类写完后, 关系代码写在DBConetx类文件中

    配置一:

    1对0/1对1关系----Configuring a Required-to-Optional Relationship (One-to–Zero-or-One)

      个人理解:单向关联??强弱关联??(Required有强引用,需要的意思,Optional有弱引用,可选的意思)

      下代码分别:  配置主键; 配置主键及外键

    // Configure the primary key for the OfficeAssignment 
    modelBuilder.Entity<OfficeAssignment>() 
        .HasKey(t => t.InstructorID); 
     
    // Map one-to-zero or one relationship 
    modelBuilder.Entity<OfficeAssignment>() 
        .HasRequired(t => t.Instructor) 
        .WithOptional(t => t.OfficeAssignment);

    配置二:

    1对1关系----Configuring a Relationship Where Both Ends Are Required (One-to-One)

      个人理解:双向关联,但不确定这里的 Required/Optianal 是什么意思

      HasRequired --- WithRequiredPrincipa or WithRequiredDependent  (both ends of the relationship are required)

      HasOptianal --- WithOptionalPrincipal or WithOptionalDependent    (both ends of the relationship are required)

    // Configure the primary key for the OfficeAssignment 
    modelBuilder.Entity<OfficeAssignment>() 
        .HasKey(t => t.InstructorID); 
     
    modelBuilder.Entity<Instructor>() 
        .HasRequired(t => t.OfficeAssignment) 
        .WithRequiredPrincipal(t => t.Instructor);

    配置三:

    多对多----Configuring a Many-to-Many Relationship

      文中说明:CourseInstructor table is created with Course_CourseID and Instructor_InstructorID columns

    modelBuilder.Entity<Course>() 
        .HasMany(t => t.Instructors) 
        .WithMany(t => t.Courses)

      文中说明:generates the CourseInstructor table with CourseID and InstructorID columns

    modelBuilder.Entity<Course>() 
        .HasMany(t => t.Instructors) 
        .WithMany(t => t.Courses) 
        .Map(m => 
        { 
            m.ToTable("CourseInstructor"); 
            m.MapLeftKey("CourseID"); 
            m.MapRightKey("InstructorID"); 
        });

    疑问

    配置四:

    配置导航属性----Configuring a Relationship with One Navigation Property

      区别:   与配置一相比,少了WitOptional(t=>tofficeAssignment)

          与配置二相比,WithRequiredPrincipal() 中少了t => t.Instructor

      官方说明:if want a one-to-one relationship between Instructor and OfficeAssignment, where you have a navigation property on only the Instructor type

    // Configure the primary Key for the OfficeAssignment 
    modelBuilder.Entity<OfficeAssignment>() 
        .HasKey(t => t.InstructorID); 
     
    modelBuilder.Entity<Instructor>() 
        .HasRequired(t => t.OfficeAssignment) 
        .WithRequiredPrincipal();

    配置五:

    级联删除----Cascade Delete

  • 相关阅读:
    华为超级应用联合创新计划启动,共同打造极致用户体验
    华为P20无敌拍摄能力开放 如何即刻获得?
    两千万次服务的背后,华为终端开放实验室到底做了什么?
    HUAWEI HiAI亮相华为开发者生态大会 助力应用AI开发实现加速度
    搜狐新闻APP是如何使用HUAWEI DevEco IDE快速集成HUAWEI HiAI Engine
    旅行助手:重新定义旅行
    世界更清晰,搜狐新闻客户端集成HUAWEI HiAI 亮相荣耀Play发布会!
    Android和设置alpha(图像)透明度
    Android应用开发欢迎界面不想显示最上面的LOGO
    聊天页面输入框和发送按钮的布局问题 Android
  • 原文地址:https://www.cnblogs.com/MrGrz/p/5786387.html
Copyright © 2011-2022 走看看