zoukankan      html  css  js  c++  java
  • 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类型

  • 相关阅读:
    Java之冒泡完整理解
    Java 之数组的复制,
    python 小白之路(跳动的球)
    小白之旅,Python运算符
    在学习枯燥的Java中遇见美丽的Jframe,窗体中的单选按钮(JRadioButton)
    自定义的无数据提示界面
    北京地铁站经纬度集合(包含大部分)
    自定义UItextFiled,限制TextFiled 的输入长度
    消息发送机制的利用
    app 后台持续定位
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6641751.html
Copyright © 2011-2022 走看看