EF6学习笔记(六续) 复杂数据模型建表测试

测试以下几种模型关系:

1对1或0  、 1对多  、 多对多

1 对 1 或 0

如果直接定义两个模型,相互直接增加导航属性,会提示错误,必须为这个对应关系设定主副关系;

    public class Team
    {
        public int ID { get; set; }
        public string Name { get; set; }  
public virtual Address Address { get; set; } } public class Address { [ForeignKey("Team")] public int ID { get; set; } public string City { get; set; } public string DetailAddress { get; set; }
public virtual Team Team { get; set; } }

1 对 多  ( 0或1 对多):

    public class Model1
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Model2> Model2s { get; set; }
    }


    public class Model2
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

主表中定义子表的ICollection<T>导航属性行即可实现EF自动创建两个表,并在子表中自动增加 Model1_ID 列;(列名命名格式为:<类名>+下划线+<主键名>)

(并且自动为外键):(外键命名规则:FK_ + <子表全名> + 下划线 + <主表全名> + 下划线 + 列名)

也就是说在子表中并不一定需要主表的导航属性就可以定义主次表1对多的关系;

如果希望手动来定义外键,则必须在子表中明确增加主表的导航属性:

    public class Model1
    {       
        public int ID { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Model2> Model2s { get; set; }
    }


    public class Model2
    {     
        public int ID { get; set; }
        [ForeignKey("Model1")]
        public int Model1ID { get; set; }

        public string Name { get; set; }
        public virtual Model1 Model1 { get; set; }
    }

此时,外键列名就为自己设定的名称,外键也会根据命名规则,最后部分变为外键列名;
上面2个方式除了命名规则不同以外,还有个不同,第2种方式由于外键对应的列是int 数值型的,那么就不可以为空;即只能是多对1,不可以是多对0;

当然也可以在数值型后面加 ? ,来定义可以为空;(用来实现 0或1 对 多

注意:在数据库升级迁移过程中,把一个字段从可以为空变为不可以为空,如果这个表中存在数据而且该字段存在为空的行,则数据库升级将不会成功!

 

多对多

    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Course> Courses { get; set; }
    }

    public class Course
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Student> Students { get; set; }
    }

多对多表是在两个模型相互加 ICollection<T> 导航属性;
EF会自动增加一个新表用来存放对应关系表;

原文地址:https://www.cnblogs.com/jacky-zhang/p/7448449.html