private async Task UnEnrollSteveFromCourse(ManyToManyContext ctx) { StudentCourse steveAndDnp = ctx.Students. Where(s => s.StudentNum == 123456). SelectMany(student => student.StudentCourses). First(studentCourse => studentCourse.Course.CourseCode.Equals("IT-SDJ2-A20")); ctx.Remove(steveAndDnp); await ctx.SaveChangesAsync(); }
private static async Task AddOneSteve(ManyToManyContext ctx) { Student s = new Student { FirstName = "Steve", LastName = "Doe", Email = "*****@*****.**", StudentNum = 123456 }; await ctx.Students.AddAsync(s); await ctx.SaveChangesAsync(); }
private static async Task WhichCoursesIsSteveEnrolledIn(ManyToManyContext ctx) { List <Course> courses = await ctx.Students .Where(s => s.StudentNum == 123456) .SelectMany(student => student.StudentCourses) .Select(studentCourse => studentCourse.Course) .ToListAsync(); foreach (var course in courses) { Console.WriteLine(course); } }
private static async Task EnrollSteveInDNP(ManyToManyContext ctx, string course) { Student steve = await ctx.Students.FirstAsync(s => s.StudentNum == 123456); Course dnp = await ctx.Courses.FirstAsync(c => c.CourseCode.Equals(course)); StudentCourse sc = new StudentCourse { Course = dnp, Student = steve }; ctx.Set <StudentCourse>().Add(sc); await ctx.SaveChangesAsync(); }
protected override void Seed(ManyToManyContext context) { base.Seed(context); ChangesDate = new DateTime(2010, 1, 1); context.RemoveRange(context.ChangeTracker.Entries().Where(e => e.Entity is EntityThree).Select(e => e.Entity)); context.RemoveRange(context.ChangeTracker.Entries().Where(e => e.Entity is EntityTwo).Select(e => e.Entity)); context.RemoveRange(context.ChangeTracker.Entries().Where(e => e.Entity is EntityOne).Select(e => e.Entity)); context.RemoveRange(context.ChangeTracker.Entries().Where(e => e.Entity is EntityCompositeKey).Select(e => e.Entity)); context.RemoveRange(context.ChangeTracker.Entries().Where(e => e.Entity is EntityRoot).Select(e => e.Entity)); context.SaveChanges(); var tableNames = new List <string> { "EntityCompositeKeys", "EntityOneEntityTwo", "EntityOnes", "EntityTwos", "EntityThrees", "EntityRoots", "EntityRootEntityThree", "JoinCompositeKeyToLeaf", "EntityCompositeKeyEntityRoot", "JoinOneSelfPayload", "JoinOneToBranch", "JoinOneToThreePayloadFull", "JoinOneToThreePayloadFullShared", "JoinOneToTwo", "JoinThreeToCompositeKeyFull", "EntityTwoEntityTwo", "EntityCompositeKeyEntityTwo", "JoinTwoToThree", }; foreach (var tableName in tableNames) { context.Database.ExecuteSqlRaw($"ALTER TABLE [{tableName}] SET (SYSTEM_VERSIONING = OFF)"); context.Database.ExecuteSqlRaw($"ALTER TABLE [{tableName}] DROP PERIOD FOR SYSTEM_TIME"); context.Database.ExecuteSqlRaw($"UPDATE [{tableName + "History"}] SET PeriodStart = '2000-01-01T01:00:00.0000000Z'"); context.Database.ExecuteSqlRaw($"UPDATE [{tableName + "History"}] SET PeriodEnd = '2020-07-01T07:00:00.0000000Z'"); context.Database.ExecuteSqlRaw($"ALTER TABLE [{tableName}] ADD PERIOD FOR SYSTEM_TIME ([PeriodStart], [PeriodEnd])"); context.Database.ExecuteSqlRaw( $"ALTER TABLE [{tableName}] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[{tableName + "History"}]))"); } }
static void TryManyToManyContext() { using (var context = new ManyToManyContext()) { Artist artist1 = new Artist() { FirstName = "Mihai", LastName = "Luca", }; Album album1 = new Album { Name = "Blue Flower", Date = DateTime.Now }; AlbumArtist albumArtist1 = new AlbumArtist { Album = album1, Artist = artist1 }; context.Artists.Add(artist1); context.Albums.Add(album1); context.AlbumArtists.Add(albumArtist1); context.SaveChanges(); foreach (var artist in context.Artists) { Console.WriteLine($"Name:{artist.FirstName} {artist.LastName}"); foreach (var albumArtist in artist.AlbumArtists) { Console.WriteLine($"Name: {albumArtist.Album.Name} Date: {albumArtist.Album.Date}"); } } } }
private static async Task AddTwoCourses(ManyToManyContext ctx) { Course sdj2 = new Course { Abbreviation = "SDJ2", Name = "Software Development with UML and Java 2", Semester = 2, CourseCode = "IT-SDJ2-A20", ECTS = 10 }; Course dnp1 = new Course { Abbreviation = "DNP1", Name = "I forgot the actual name", Semester = 3, CourseCode = "IT-DNP1Y-A20", ECTS = 5 }; await ctx.Courses.AddAsync(sdj2); await ctx.Courses.AddAsync(dnp1); await ctx.SaveChangesAsync(); }
static async Task Main(string[] args) { using (ManyToManyContext context = new ManyToManyContext()) await WhichCoursesIsSteveEnrolledIn(context); }