Skip to content

Commit

Permalink
fix(views): do not analyze Indexes or FKs on Views
Browse files Browse the repository at this point in the history
  • Loading branch information
jayharris committed Sep 16, 2020
1 parent c4d736f commit 1495756
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 16 deletions.
Expand Up @@ -5,7 +5,7 @@
<TargetFrameworks>netstandard2.1</TargetFrameworks>
<AssemblyName>Aranasoft.Cobweb.EntityFrameworkCore.Validation</AssemblyName>
<RootNamespace>Aranasoft.Cobweb.EntityFrameworkCore.Validation</RootNamespace>
<VersionPrefix>1.30.2</VersionPrefix>
<VersionPrefix>1.30.3</VersionPrefix>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>

Expand Down
Expand Up @@ -37,11 +37,11 @@ public class SchemaValidator {

validationErrors.AddRange(ValidateColumns(databaseModel, persistedType, validationOptions));

if (validationOptions.ValidateIndexes) {
if (validationOptions.ValidateIndexes && persistedType.FindAnnotation(RelationalAnnotationNames.ViewDefinition) == null) {
validationErrors.AddRange(ValidateIndexes(databaseModel, persistedType));
}

if (validationOptions.ValidateForeignKeys) {
if (validationOptions.ValidateForeignKeys && persistedType.FindAnnotation(RelationalAnnotationNames.ViewDefinition) == null) {
validationErrors.AddRange(ValidateForeignKeys(databaseModel, persistedType));
}
}
Expand Down
Expand Up @@ -16,6 +16,7 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
base.OnModelCreating(builder);

builder.ApplyConfiguration(new ViewBasedEntityMapping());
builder.ApplyConfiguration(new TableBasedEntityMapping());
}
}
}
Expand Up @@ -97,7 +97,13 @@ public override void Up()
.WithColumn("Value", col => col.AsStringMax().Nullable())
;

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Email FROM AspNetUsers");
Create.Table("TableBasedEntity")
.WithColumn("Id", col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("RoleId", col => col.AsInt32().NotNullable().Indexed("IX_TableBasedEntity_RoleId"))
;

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Field, RoleId FROM TableBasedEntity");
}

public override void Down()
Expand Down
Expand Up @@ -105,7 +105,15 @@ public override void Up()
.WithColumn("Value", col => col.AsStringMax().Nullable())
;

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Email FROM AspNetUsers");
Create.Table("TableBasedEntity")
.WithColumn("Id", col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("RoleId", col => col.AsInt32().NotNullable()
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId", "AspNetRoles", "Id")
.OnDelete(Rule.Cascade))
;

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Field, RoleId FROM TableBasedEntity");
}

public override void Down()
Expand Down
Expand Up @@ -107,13 +107,22 @@ public override void Up()
;

Create.Table("AspNetUserTokens")
.WithColumn("UserId",
col => col.AsInt32().NotNullable().PrimaryKey("PK_AspNetUserTokens")
.ForeignKey("FK_AspNetUserTokens_AspNetUsers_UserId", "AspNetUsers", "Id")
.OnDelete(Rule.Cascade))
.WithColumn("LoginProvider", col => col.AsString(450).NotNullable().PrimaryKey("PK_AspNetUserTokens"))
.WithColumn("Name", col => col.AsString(450).NotNullable().PrimaryKey("PK_AspNetUserTokens"))
.WithColumn("Value", col => col.AsStringMax().Nullable())
.WithColumn("UserId",
col => col.AsInt32().NotNullable().PrimaryKey("PK_AspNetUserTokens")
.ForeignKey("FK_AspNetUserTokens_AspNetUsers_UserId", "AspNetUsers", "Id")
.OnDelete(Rule.Cascade))
.WithColumn("LoginProvider", col => col.AsString(450).NotNullable().PrimaryKey("PK_AspNetUserTokens"))
.WithColumn("Name", col => col.AsString(450).NotNullable().PrimaryKey("PK_AspNetUserTokens"))
.WithColumn("Value", col => col.AsStringMax().Nullable())
;

Create.Table("TableBasedEntity")
.WithColumn("Id", col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("RoleId", col => col.AsInt32().NotNullable()
.Indexed("IX_TableBasedEntity_RoleId")
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId", "AspNetRoles", "Id")
.OnDelete(Rule.Cascade))
;
}

Expand Down
Expand Up @@ -116,7 +116,16 @@ public override void Up()
.WithColumn("Value", col => col.AsStringMax().Nullable())
;

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Email FROM AspNetUsers");
Create.Table("TableBasedEntity")
.WithColumn("Id", col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("RoleId", col => col.AsInt32().NotNullable()
.Indexed("IX_TableBasedEntity_RoleId")
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId", "AspNetRoles", "Id")
.OnDelete(Rule.Cascade))
;

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Field, RoleId FROM TableBasedEntity");
}

public override void Down()
Expand Down
Expand Up @@ -116,7 +116,16 @@ public override void Up()
.WithColumn("Value", col => col.AsStringMax().Nullable())
;

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Email FROM AspNetUsers");
Create.Table("TableBasedEntity")
.WithColumn("Id", col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("RoleId", col => col.AsInt32().NotNullable()
.Indexed("IX_TableBasedEntity_RoleId")
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId", "AspNetRoles", "Id")
.OnDelete(Rule.Cascade))
;

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Field, RoleId FROM TableBasedEntity");
}

public override void Down()
Expand Down
Expand Up @@ -116,7 +116,16 @@ public override void Up()
.WithColumn("Value", col => col.AsStringMax().Nullable())
;

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Email FROM AspNetUsers");
Create.Table("TableBasedEntity")
.WithColumn("Id", col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("RoleId", col => col.AsInt32().NotNullable()
.Indexed("IX_TableBasedEntity_RoleId")
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId", "AspNetRoles", "Id")
.OnDelete(Rule.Cascade))
;

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Field, RoleId FROM TableBasedEntity");
}

public override void Down()
Expand Down
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Identity;

namespace Aranasoft.Cobweb.EntityFrameworkCore.Validation.Tests.Support {
public class TableBasedEntity {
public int Id { get; set; }
[MaxLength(256)]
public string Field { get; set; }
[Required]
public IdentityRole<int> Role { get; set; }
public ViewBasedEntity ViewEntity { get; set; }
}
}
@@ -0,0 +1,11 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Aranasoft.Cobweb.EntityFrameworkCore.Validation.Tests.Support {
public class TableBasedEntityMapping : IEntityTypeConfiguration<TableBasedEntity>
{
public void Configure(EntityTypeBuilder<TableBasedEntity> builder) {
builder.Ignore(entity => entity.ViewEntity);
}
}
}
@@ -1,10 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Identity;

namespace Aranasoft.Cobweb.EntityFrameworkCore.Validation.Tests.Support {
public class ViewBasedEntity {
public int Id { get; set; }
[MaxLength(256)]
public string Email { get; set; }
public string Field { get; set; }
public IdentityRole<int> Role { get; set; }
public TableBasedEntity TableEntity { get; set; }
}
}
Expand Up @@ -6,6 +6,9 @@ public class ViewBasedEntityMapping : IQueryTypeConfiguration<ViewBasedEntity>
{
public void Configure(QueryTypeBuilder<ViewBasedEntity> builder) {
builder.ToView("ViewBasedEntities");
builder.Property<int>("RoleId");
builder.HasOne(entity => entity.TableEntity).WithOne().HasForeignKey<ViewBasedEntity>(entity => entity.Id).HasPrincipalKey<TableBasedEntity>(entity => entity.Id);
builder.HasOne(entity => entity.Role).WithMany().HasForeignKey("RoleId");
}
}
}
Expand Up @@ -38,6 +38,7 @@
<Compile Include="..\entityframeworkcore2.validation.tests\Support\SqlServer\SqlServerLocalDbFixture.cs" Link="Support\SqlServer\SqlServerLocalDbFixture.cs" />
<Compile Include="..\entityframeworkcore2.validation.tests\Support\SqlServer\SqlServerMigrationsFixture.cs" Link="Support\SqlServer\SqlServerMigrationsFixture.cs" />
<Compile Include="..\entityframeworkcore2.validation.tests\Support\SqlServer\SqlServerTestingProcessor.cs" Link="Support\SqlServer\SqlServerTestingProcessor.cs" />
<Compile Include="..\entityframeworkcore2.validation.tests\Support\TableBasedEntity.cs" Link="Support\TableBasedEntity.cs" />
<Compile Include="..\entityframeworkcore2.validation.tests\Support\ViewBasedEntity.cs" Link="Support\ViewBasedEntity.cs" />
<Compile Include="..\entityframeworkcore2.validation.tests\Support\XUnit\ConditionalFactAttribute.cs" Link="Support\XUnit\ConditionalFactAttribute.cs" />
<Compile Include="..\entityframeworkcore2.validation.tests\Support\XUnit\ConditionalFactAttributeDiscoverer.cs" Link="Support\XUnit\ConditionalFactAttributeDiscoverer.cs" />
Expand Down
@@ -0,0 +1,10 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Aranasoft.Cobweb.EntityFrameworkCore.Validation.Tests.Support {
public class TableBasedEntityMapping : IEntityTypeConfiguration<TableBasedEntity>
{
public void Configure(EntityTypeBuilder<TableBasedEntity> builder) {
}
}
}
Expand Up @@ -6,6 +6,7 @@ public class ViewBasedEntityMapping : IEntityTypeConfiguration<ViewBasedEntity>
{
public void Configure(EntityTypeBuilder<ViewBasedEntity> builder) {
builder.ToView("ViewBasedEntities");
builder.HasOne(viewBasedEntity => viewBasedEntity.TableEntity).WithOne(tableBasedEntity => tableBasedEntity.ViewEntity).HasForeignKey<ViewBasedEntity>(entity => entity.Id);
}
}
}

0 comments on commit 1495756

Please sign in to comment.