Презентация

EntityFramework Relations - Databases Advanced - Entity Framework - октомври 2016

 

Материали от темата

Съдържание на документа

Entity Framework:
Relations

SoftUni Team

Technical Trainers

Software University

http://softuni.bg

Database

Applications


Table of Contents

Configuring table relationships

One-to-Zero-or-One

One-to-Many

Many-to-Many 

Inverse properties

Inheritance Strategies

Table per Hierarchy (TPH)

Table per Type (TPT)

Table per Concrete Type (TPC)



2


3



sli.do
#Entity

Questions


4

Table Relationships


5

One-to-Zero-or-One 
(using attributes) 


6

public class Student

    {

        public int StudentId { get; set; }

        public string StudentName { get; set; }



        public virtual StudentAddress Address { get; set; }

    }

 public class StudentAddress

    {

        [ForeignKey("Student")]

        public int StudentAddressId { get; set; }



        public string Address1 { get; set; }

        public string City { get; set; }

        public int Zipcode { get; set; }

        public virtual Student Student { get; set; }

    }


7

One-to-Zero-or-One 
(using Fluent API) 


8

modelBuilder.Entity<Student>()



                .HasOptional(student => student.Address)

	OR

	.HasRequired(student => student.Address)



                .WithRequired(address => address.Student);


9

One-to-Many
(using attributes) 


10

class Student

    {

        public int StudentId { get; set; }

        public string StudentName { get; set; }

        public int StandardRefId { get; set; }



        [ForeignKey("StandardRefId")]

        public virtual Standard Standard { get; set; }    }

class Standard

    {

        public Standard()

        {

            Students = new HashSet<Student>();  

        }

        public int StandardId { get; set; }

        public string Description { get; set; }	

        public virtual ICollection<Student> Students { get; set; }

    }


11

One-to-Many
(using Fluent API) 


12

modelBuilder.Entity<Student>()

                    .HasRequired<Standard>(s => s.Standard)

                    .WithMany(s => s.Students)

                    .HasForeignKey(s => s.StandardRefId);


13

Many-to-Many
(using attributes) 


14

public class Student

        public Student()

        {

            this.Courses = new HashSet<Course>();

        }



        public int StudentId { get; set; }

        [Required]

        public string StudentName { get; set; }

        public virtual ICollection<Course> Courses

	 { get; set; }

public class Course

        public Course()

        {

            this.Students = new HashSet<Student>();

        }



        public int CourseId { get; set; }

        public string CourseName { get; set; }

        public virtual ICollection<Student> Students 

	{ get; set; }

    


15

Many-to-Many
(using Fluent API) 


16

modelBuilder.Entity<Student>()

               .HasMany<Course>(s => s.Courses)

               .WithMany(c => c.Students)

               .Map(cs =>

               {

                   cs.MapLeftKey("StudentRefId");

                   cs.MapRightKey("CourseRefId");

                   cs.ToTable("StudentCourse");

               });


17

Inverse properties


18

public class Person

        public int Id { get; set; }



        [Required]

        public string Name { get; set; }



        public Town BornTown { get; set; }



        public Town LivingTown { get; set; }

public class Town

        public Town()

        {

            this.BornPeople = new HashSet<Person>();

            this.LivingPeople = new List<Person>();

        }

        public int Id { get; set; }

        public ICollection<Person> BornPeople { get; set; }

        public ICollection<Person> LivingPeople { get; set; }

    }


19

Inheritance Strategies


20

Table per Hierarchy (TPH)



21

modelBuilder.Entity<ParentClass>()

            .Map<ChildClassOne>(m => 

			m.Requires(“NameOfDiscriminator")

			    .HasValue(“valueForChildClassOne”))

            .Map<ChildClassTwo>(m => 

			m.Requires("NameOfDiscriminator")

			    .HasValue(“valueForChildClassTwo”));

Modifying the default setting for TPH


22

Table per Type (TPT)
 


23

We can create a TPT mapping simply by placing Table attribute on the subclasses to specify the mapped table name



OR



modelBuilder.Entity<ChildClassOne>().ToTable(“TableNameOfChildClassOne”);

modelBuilder.Entity<ChildClassTwo>().ToTable(“TableNameOfChildClassTwo”);

Modifying the classes to use TPT


24

Table per Concrete Type (TPC)



25

modelBuilder.Entity<ChildClassOne>().Map(m =>

            {

                m.MapInheritedProperties();

                m.ToTable(" TableNameOfChildClassTwo");

            });

Modifying the classes to use TPC

modelBuilder.Entity<ChildClassTwo>().Map(m =>

            {

                m.MapInheritedProperties();

                m.ToTable(“TableNameOfChildClassOne");

            });


26

Attach [DatabaseGenerated(DatabaseGenerationOption.None)]

to the key of the base class. 



OR

modelBuilder.Entity<ParentClass>() 

            .Property(p => p.”Id”) 

            .HasDatabaseGenerationOption(

		DatabaseGenerationOption.None);







Modifying the classes to use TPC 


https://softuni.bg/courses/


License

This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International" license

28

Attribution: this work may contain portions from

"Databases" course by Telerik Academy under CC-BY-NC-SA license


Free Trainings @ Software University

Software University Foundation – softuni.org

Software University – High-Quality Education, Profession and Job for Software Developers

softuni.bg 

	Software University @ Facebook

	facebook.com/SoftwareUniversity

	Software University @ YouTube

	youtube.com/SoftwareUniversity

	Software University Forums – forum.softuni.bg