예제 #1
0
    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"}]))");
        }
    }
예제 #2
0
        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}");
                    }
                }
            }
        }