Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Represents the **NuGet** versions.

## v3.2.1
- *Fixed:* SQL Server schema inference no longer throws `Database data type 'x' does not have corresponding .NET type mapping defined` for `geography`, `geometry`, `hierarchyid`, `xml` and `vector` columns; they map to `string`. Previously a single column of one of these types in any table failed the `Data`, `Reset` and `Inspect` commands for the whole database.

## v3.2.0
- *Enhancement:* The `MigrationCommand.Execute` has been enhanced to support the execution of YAML/JSON data files. Raw SQL must _now_ be prefixed by `>` to explicitly differentiate from a file path.
- *Enhancement:* The `MigrationCommand.Inspect` has been enhanced to also report whether each column has been identified as a JSON column (i.e. `DbColumnSchema.IsJson`).
Expand Down
4 changes: 4 additions & 0 deletions src/DbEx.SqlServer/SqlServerSchemaConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ public override string ToDotNetTypeName(DbColumnSchema schema)
"TINYINT" => "byte",
"REAL" => "float",
"UNIQUEIDENTIFIER" => "Guid",
// CLR and other non-primitive types surface as their string representation (WKT, path, XML, JSON array);
// SQL Server converts the string implicitly on insert. Without a mapping, a single column of one of
// these types in any table would fail the whole schema inference (Data, Reset and Inspect).
"GEOGRAPHY" or "GEOMETRY" or "HIERARCHYID" or "XML" or "VECTOR" => "string",
_ => throw new InvalidOperationException($"Database data type '{dbType}' does not have corresponding .NET type mapping defined."),
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Create table: [Test].[ClrTypes]

BEGIN TRANSACTION

CREATE TABLE [Test].[ClrTypes] (
[ClrTypesId] INT NOT NULL PRIMARY KEY,
[Location] GEOGRAPHY NULL,
[Shape] GEOMETRY NULL,
[Path] HIERARCHYID NULL,
[Document] XML NULL
)

COMMIT TRANSACTION
25 changes: 25 additions & 0 deletions tests/DbEx.Test/DatabaseSchemaTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,31 @@ public async Task SqlServerSelectSchema()
Assert.IsNull(col.ForeignTable);
Assert.IsNull(col.ForeignColumn);
Assert.IsNotNull(col.DefaultValue);

// [Test].[ClrTypes] - CLR and other non-primitive types map to string.
tab = tables.Where(x => x.Name == "ClrTypes").SingleOrDefault();
Assert.IsNotNull(tab);
Assert.AreEqual(5, tab.Columns.Count);

col = tab.Columns[1];
Assert.AreEqual("Location", col.Name);
Assert.AreEqual("geography", col.Type);
Assert.AreEqual("string", col.DotNetType);

col = tab.Columns[2];
Assert.AreEqual("Shape", col.Name);
Assert.AreEqual("geometry", col.Type);
Assert.AreEqual("string", col.DotNetType);

col = tab.Columns[3];
Assert.AreEqual("Path", col.Name);
Assert.AreEqual("hierarchyid", col.Type);
Assert.AreEqual("string", col.DotNetType);

col = tab.Columns[4];
Assert.AreEqual("Document", col.Name);
Assert.AreEqual("xml", col.Type);
Assert.AreEqual("string", col.DotNetType);
}

[Test]
Expand Down