• EF Code-First 学习之旅 一对多的关系



    public
    class Student { public Student() { } public int StudentId { get; set; } public string StudentName { get; set; } public virtual Standard Standard { get; set; } } public class Standard { public Standard() { Students = new List<Student>(); } public int StandardId { get; set; } public string Description { get; set; } public virtual ICollection<Student> Students { get; set; } }

    上面的代码中,Student实体包含导航属性Standard,Standard实体包含集合导航属性Student,Code First的默认规则为1对多的关系

     指定外键

    public class Student
    {
        public Student() { }
    
        public int StudentId { get; set; }
        public string StudentName { get; set; }
    
            public int StdandardRefId { get; set; }
            
        [ForeignKey("StandardRefId")]
        public virtual Standard Standard { get; set; }
    }
           
    public class Standard
    {
        public Standard()
        {
            Students = new List<Student>();
        }
        public int StandardId { get; set; }
        public string Description { get; set; }
    
        public virtual ICollection<Student> Students { get; set; }
    }

     

    Fluent API配置

    public class Student
    {
        public Student(){ }
    
        public int StudentId { get; set; }
        public string StudentName { get; set; }
    
        public int StandardId { get; set; }
    
        public virtual Standard Standard { get; set; }
    }
           
    public class Standard
    {
        public Standard()
        {
            StudentsList = new List<Student>();
        }
        public int StandardId { get; set; }
        public string Description { get; set; }
    
        public virtual ICollection<Student> Students { get; set; }
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
            //one-to-many 
            modelBuilder.Entity<Student>()
                        .HasRequired<Standard>(s => s.Standard) // Student entity requires Standard 
                        .WithMany(s => s.Students); // Standard entity includes many Students entities
    
    }

     如果外键不符合默认规则

    public class Student
    {
        public Student(){ }
    
        public int StudentId { get; set; }
        public string StudentName { get; set; }
    
        //StdId is not following code first conventions name
        public int StdId { get; set; }
    
        public virtual Standard Standard { get; set; }
    }
           
    public class Standard
    {
        public Standard()
        {
            StudentsList = new List<Student>();
        }
        public int StandardId { get; set; }
        public string Description { get; set; }
    
        public virtual ICollection<Student> Students { get; set; }
    }

    可以如下配置

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
            //one-to-many 
            modelBuilder.Entity<Student>()
                        .HasRequired<Standard>(s => s.Standard)
                        .WithMany(s => s.Students)
                        .HasForeignKey(s => s.StdId);
    
    }

    modelBuilder.Entity<Student>().HasRequired<Standard>(s => s.Standard)

    表明Student必须包含Standard导航属性,

    .WithMany(s => s.Students).HasForeignKey(s => s.StdId)表明Standard有多个Student,外键名为StdId。

    另一种方法

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
            //configure one-to-many
            modelBuilder.Entity<Standard>()
                        .HasMany<Student>(s => s.Students) Standard has many Students
                        .WithRequired(s => s.Standard)  Student require one Standard
                        .HasForeignKey(s => s.StdId);Student includes specified foreignkey property name for Standard
    }

    外键可空的配置

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
            //one-to-many 
            modelBuilder.Entity<Student>()
                        .HasOptional<Standard>(s => s.Standard)
                        .WithMany(s => s.Students)
                        .HasForeignKey(s => s.StdId);
    
    }
       

    配置如发生如下异常

    One or more validation errors were detected during model generation:

    Domain.Student_Standard: : Multiplicity conflicts with the referential constraint in Role 'Student_Standard_Target' in relationship 'Student_Standard'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.

    则需要配置外键为nullable类型

  • 相关阅读:
    C++ 如何重复利用一个内存地址块
    C与C++在const用法上的区别
    C++ 与设计模式学习(其一)
    C/C++ 关于生成静态库(lib)/动态库(dll)文件如何使用(基于windows基础篇)
    c/c++----网站及其后门(CGI应用程序)
    C/C++深度copy和浅copy
    C/C++ 一段代码区分数组指针|指针数组|函数指针|函数指针数组
    C/C++ 如何劫持别人家的命令||函数||程序(只能对于window而言)
    C++继承与派生(原理归纳)
    Linux下如何查看自己的服务器有没有无线网卡
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6641751.html
Copyright © 2020-2023  润新知