protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.Entity <Asset>()
            .HasIndex(a => new { a.Name, a.Version });

            builder.Entity <Channel>().HasIndex(c => c.Name).IsUnique();

            builder.Entity <BuildChannel>()
            .HasKey(
                bc => new
            {
                bc.BuildId,
                bc.ChannelId
            });

            builder.Entity <BuildChannel>()
            .HasOne(bc => bc.Build)
            .WithMany(b => b.BuildChannels)
            .HasForeignKey(bc => bc.BuildId);

            builder.Entity <BuildChannel>()
            .HasOne(bc => bc.Channel)
            .WithMany(c => c.BuildChannels)
            .HasForeignKey(bc => bc.ChannelId);

            builder.Entity <BuildDependency>()
            .HasKey(d => new { d.BuildId, d.DependentBuildId });

            builder.Entity <BuildDependency>()
            .HasOne(d => d.Build)
            .WithMany()
            .HasForeignKey(d => d.BuildId)
            .OnDelete(DeleteBehavior.Restrict);

            builder.Entity <BuildDependency>()
            .HasOne(d => d.DependentBuild)
            .WithMany()
            .HasForeignKey(d => d.DependentBuildId)
            .OnDelete(DeleteBehavior.Restrict);

            builder.Entity <ApplicationUserPersonalAccessToken>()
            .HasIndex(
                t => new
            {
                t.ApplicationUserId,
                t.Name
            })
            .IsUnique();

            builder.Entity <DefaultChannel>()
            .HasIndex(
                dc => new
            {
                dc.Repository,
                dc.Branch,
                dc.ChannelId
            })
            .IsUnique();

            builder.Entity <SubscriptionUpdate>()
            .HasOne(su => su.Subscription)
            .WithOne()
            .HasForeignKey <SubscriptionUpdate>(su => su.SubscriptionId)
            .OnDelete(DeleteBehavior.Restrict);

            builder.ForSqlServerIsSystemVersioned <SubscriptionUpdate, SubscriptionUpdateHistory>("6 MONTH");

            builder.Entity <SubscriptionUpdateHistory>().HasIndex("SubscriptionId", "SysEndTime", "SysStartTime");

            builder.Entity <Repository>().HasKey(r => new { r.RepositoryName });

            builder.Entity <RepositoryBranch>()
            .HasKey(
                rb => new
            {
                rb.RepositoryName,
                rb.BranchName
            });

            builder.Entity <RepositoryBranch>()
            .HasOne(rb => rb.Repository)
            .WithMany(r => r.Branches)
            .HasForeignKey(rb => new { rb.RepositoryName });

            builder.Entity <RepositoryBranchUpdate>()
            .HasKey(
                ru => new
            {
                ru.RepositoryName,
                ru.BranchName
            });

            builder.Entity <RepositoryBranchUpdate>()
            .HasOne(ru => ru.RepositoryBranch)
            .WithOne()
            .HasForeignKey <RepositoryBranchUpdate>(
                ru => new
            {
                ru.RepositoryName,
                ru.BranchName
            })
            .OnDelete(DeleteBehavior.Restrict);

            builder.Entity <GoalTime>()
            .HasKey(
                gt => new
            {
                gt.DefinitionId,
                gt.ChannelId
            });

            builder.Entity <GoalTime>()
            .HasOne(gt => gt.Channel)
            .WithMany()
            .HasForeignKey(gt => gt.ChannelId);

            builder.ForSqlServerIsSystemVersioned <RepositoryBranchUpdate, RepositoryBranchUpdateHistory>("6 MONTH");

            builder.Entity <RepositoryBranchUpdateHistory>()
            .HasKey(
                ru => new
            {
                ru.RepositoryName,
                ru.BranchName
            });

            builder.Entity <RepositoryBranchUpdateHistory>()
            .HasIndex("RepositoryName", "BranchName", "SysEndTime", "SysStartTime");

            builder.HasDbFunction(() => JsonExtensions.JsonValue("", ""))
            .HasTranslation(args => SqlFunctionExpression.Create("JSON_VALUE", args, typeof(string), null));
        }
Esempio n. 2
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.Entity <Asset>()
            .HasIndex(a => new { a.Name, a.Version });

            builder.Entity <Channel>().HasIndex(c => c.Name).IsUnique();

            builder.Entity <BuildChannel>()
            .HasKey(
                bc => new
            {
                bc.BuildId,
                bc.ChannelId
            });

            builder.Entity <BuildChannel>()
            .HasOne(bc => bc.Build)
            .WithMany(b => b.BuildChannels)
            .HasForeignKey(bc => bc.BuildId);

            builder.Entity <BuildChannel>()
            .HasOne(bc => bc.Channel)
            .WithMany(c => c.BuildChannels)
            .HasForeignKey(bc => bc.ChannelId);

            builder.Entity <BuildDependency>()
            .HasKey(d => new { d.BuildId, d.DependentBuildId });

            builder.Entity <BuildDependency>()
            .HasOne(d => d.Build)
            .WithMany()
            .HasForeignKey(d => d.BuildId)
            .OnDelete(DeleteBehavior.Restrict);

            builder.Entity <BuildDependency>()
            .HasOne(d => d.DependentBuild)
            .WithMany()
            .HasForeignKey(d => d.DependentBuildId)
            .OnDelete(DeleteBehavior.Restrict);

            builder.Entity <ChannelReleasePipeline>()
            .HasKey(crp => new { crp.ChannelId, crp.ReleasePipelineId });

            builder.Entity <ChannelReleasePipeline>()
            .HasOne(crp => crp.Channel)
            .WithMany(c => c.ChannelReleasePipelines)
            .HasForeignKey(rcp => rcp.ChannelId)
            .OnDelete(DeleteBehavior.Restrict);

            builder.Entity <ChannelReleasePipeline>()
            .HasOne(crp => crp.ReleasePipeline)
            .WithMany(rp => rp.ChannelReleasePipelines)
            .HasForeignKey(crp => crp.ReleasePipelineId)
            .OnDelete(DeleteBehavior.Restrict);

            builder.Entity <ApplicationUserPersonalAccessToken>()
            .HasIndex(
                t => new
            {
                t.ApplicationUserId,
                t.Name
            })
            .IsUnique();

            builder.Entity <DefaultChannel>()
            .HasIndex(
                dc => new
            {
                dc.Repository,
                dc.Branch,
                dc.ChannelId
            })
            .IsUnique();

            builder.Entity <SubscriptionUpdate>()
            .HasOne(su => su.Subscription)
            .WithOne()
            .HasForeignKey <SubscriptionUpdate>(su => su.SubscriptionId)
            .OnDelete(DeleteBehavior.Restrict);

            builder.ForSqlServerIsSystemVersioned <SubscriptionUpdate, SubscriptionUpdateHistory>("6 MONTH");

            builder.Entity <SubscriptionUpdateHistory>().HasIndex("SubscriptionId", "SysEndTime", "SysStartTime");

            builder.Entity <Repository>().HasKey(r => new { r.RepositoryName });

            builder.Entity <RepositoryBranch>()
            .HasKey(
                rb => new
            {
                rb.RepositoryName,
                rb.BranchName
            });

            builder.Entity <RepositoryBranch>()
            .HasOne(rb => rb.Repository)
            .WithMany(r => r.Branches)
            .HasForeignKey(rb => new { rb.RepositoryName });

            builder.Entity <RepositoryBranchUpdate>()
            .HasKey(
                ru => new
            {
                ru.RepositoryName,
                ru.BranchName
            });

            builder.Entity <RepositoryBranchUpdate>()
            .HasOne(ru => ru.RepositoryBranch)
            .WithOne()
            .HasForeignKey <RepositoryBranchUpdate>(
                ru => new
            {
                ru.RepositoryName,
                ru.BranchName
            })
            .OnDelete(DeleteBehavior.Restrict);

            builder.Entity <GoalTime>()
            .HasKey(
                gt => new
            {
                gt.DefinitionId,
                gt.ChannelId
            });

            builder.Entity <GoalTime>()
            .HasOne(gt => gt.Channel)
            .WithMany()
            .HasForeignKey(gt => gt.ChannelId);

            builder.ForSqlServerIsSystemVersioned <RepositoryBranchUpdate, RepositoryBranchUpdateHistory>("6 MONTH");

            builder.Entity <RepositoryBranchUpdateHistory>()
            .HasKey(
                ru => new
            {
                ru.RepositoryName,
                ru.BranchName
            });

            builder.Entity <RepositoryBranchUpdateHistory>()
            .HasIndex("RepositoryName", "BranchName", "SysEndTime", "SysStartTime");

            builder.HasDbFunction(() => JsonExtensions.JsonValue("", "")).HasName("JSON_VALUE").HasSchema("");

            builder.Query <SubscriptionUpdateHistoryEntry>()
            .ToQuery(
                () => SubscriptionUpdates.FromSql(
                    @"
SELECT * FROM [SubscriptionUpdates]
FOR SYSTEM_TIME ALL
")
                .Select(
                    u => new SubscriptionUpdateHistoryEntry
            {
                SubscriptionId = u.SubscriptionId,
                Action         = u.Action,
                Success        = u.Success,
                ErrorMessage   = u.ErrorMessage,
                Method         = u.Method,
                Arguments      = u.Arguments,
                Timestamp      = EF.Property <DateTime>(u, "SysStartTime")
            }));

            builder.Query <RepositoryBranchUpdateHistoryEntry>()
            .ToQuery(
                () => RepositoryBranchUpdates.FromSql(
                    @"
SELECT * FROM [RepositoryBranchUpdates]
FOR SYSTEM_TIME ALL
")
                .Select(
                    u => new RepositoryBranchUpdateHistoryEntry
            {
                Repository   = u.RepositoryName,
                Branch       = u.BranchName,
                Action       = u.Action,
                Success      = u.Success,
                ErrorMessage = u.ErrorMessage,
                Method       = u.Method,
                Arguments    = u.Arguments,
                Timestamp    = EF.Property <DateTime>(u, "SysStartTime")
            }));
        }