Skip to content
Merged
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
16 changes: 13 additions & 3 deletions src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,8 @@ protected virtual void ColumnDefinition(
return;
}

var columnType = operation.ColumnType ?? GetColumnType(schema, table, name, operation, model)!;
var columnType = operation.ColumnType ?? GetColumnType(schema, table, name, operation, model);

builder
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(name))
.Append(" ")
Expand Down Expand Up @@ -1401,7 +1402,7 @@ protected virtual void ComputedColumnDefinition(
/// <param name="operation">The column metadata.</param>
/// <param name="model">The target model which may be <see langword="null" /> if the operations exist without a model.</param>
/// <returns>The database/store type for the column.</returns>
protected virtual string? GetColumnType(
protected virtual string GetColumnType(
string? schema,
string tableName,
string name,
Expand Down Expand Up @@ -1429,7 +1430,7 @@ protected virtual void ComputedColumnDefinition(
|| table.Indexes.Any(u => u.Columns.Contains(column));
}

return Dependencies.TypeMappingSource.FindMapping(
var storeType = Dependencies.TypeMappingSource.FindMapping(
operation.ClrType,
null,
keyOrIndex,
Expand All @@ -1440,6 +1441,15 @@ protected virtual void ComputedColumnDefinition(
operation.Precision,
operation.Scale)
?.StoreType;

if (storeType != null)
{
return storeType;
}

var fullTableName = schema != null ? $"{schema}.{tableName}" : tableName;
throw new InvalidOperationException(
RelationalStrings.UnsupportedTypeForColumn(fullTableName, name, operation.ClrType?.Name ?? "unknown"));
}

/// <summary>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore.Relational/Properties/RelationalStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,9 @@
<data name="UnsupportedType" xml:space="preserve">
<value>The current provider doesn't have a store type mapping for properties of type '{clrType}'.</value>
</data>
<data name="UnsupportedTypeForColumn" xml:space="preserve">
<value>Unable to find a store type mapping for column '{table}.{column}' with CLR type '{clrType}'.</value>
</data>
<data name="UpdateConcurrencyException" xml:space="preserve">
<value>The database operation was expected to affect {expectedRows} row(s), but actually affected {actualRows} row(s); data may have been modified or deleted since entities were loaded. See https://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,7 @@ protected override void Generate(
{
Check.DebugAssert(operation.DefaultValue is not null);

var typeMapping = (columnType != null
? Dependencies.TypeMappingSource.FindMapping(operation.DefaultValue.GetType(), columnType)
: null)
var typeMapping = Dependencies.TypeMappingSource.FindMapping(operation.DefaultValue.GetType(), columnType)
?? Dependencies.TypeMappingSource.GetMappingForValue(operation.DefaultValue);

defaultValueSql = typeMapping.GenerateSqlLiteral(operation.DefaultValue);
Expand Down Expand Up @@ -1640,7 +1638,7 @@ protected override void ColumnDefinition(
return;
}

var columnType = operation.ColumnType ?? GetColumnType(schema, table, name, operation, model)!;
var columnType = operation.ColumnType ?? GetColumnType(schema, table, name, operation, model);
builder
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(name))
.Append(" ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private bool IsSpatialiteColumn(AddColumnOperation operation, IModel? model)
operation.Table,
operation.Name,
operation,
model)!);
model));

private IReadOnlyList<MigrationOperation> RewriteOperations(
IReadOnlyList<MigrationOperation> migrationOperations,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal;
Expand Down Expand Up @@ -1267,6 +1268,25 @@ FROM [sys].[default_constraints] [d]
""");
}



[ConditionalFact]
public void Invalid_column_type_for_unmappable_clr_type_throws_meaningful_exception()
{
var ex = Assert.Throws<InvalidOperationException>(() =>
Generate(
new AddColumnOperation
{
Name = "TestColumn",
Table = "TestTable",
ClrType = typeof(System.IO.FileStream), // Unmappable CLR type
ColumnType = null,
IsNullable = false
}));

Assert.Equal(RelationalStrings.UnsupportedTypeForColumn("TestTable", "TestColumn", "FileStream"), ex.Message);
}

private static void CreateGotModel(ModelBuilder b)
=> b.HasDefaultSchema("dbo").Entity(
"Person", pb =>
Expand Down
Loading