Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit b758af1

Browse files
committed
Fix NRE with AlterColumn
1 parent 12814ad commit b758af1

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/ServiceStack.OrmLite/OrmLiteSchemaModifyApi.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public enum OnFkOption
1515

1616
public static class OrmLiteSchemaModifyApi
1717
{
18+
private static void InitUserFieldDefinition(Type modelType, FieldDefinition fieldDef)
19+
{
20+
if (fieldDef.PropertyInfo == null)
21+
{
22+
fieldDef.PropertyInfo = TypeProperties.Get(modelType).GetPublicProperty(fieldDef.Name);
23+
}
24+
}
25+
1826
public static void AlterTable<T>(this IDbConnection dbConn, string command)
1927
{
2028
AlterTable(dbConn, typeof(T), command);
@@ -35,6 +43,8 @@ public static void AddColumn<T>(this IDbConnection dbConn, Expression<Func<T, ob
3543

3644
public static void AddColumn(this IDbConnection dbConn, Type modelType, FieldDefinition fieldDef)
3745
{
46+
InitUserFieldDefinition(modelType, fieldDef);
47+
3848
var command = dbConn.GetDialectProvider().ToAddColumnStatement(modelType, fieldDef);
3949
dbConn.ExecuteSql(command);
4050
}
@@ -48,6 +58,8 @@ public static void AlterColumn<T>(this IDbConnection dbConn, Expression<Func<T,
4858

4959
public static void AlterColumn(this IDbConnection dbConn, Type modelType, FieldDefinition fieldDef)
5060
{
61+
InitUserFieldDefinition(modelType, fieldDef);
62+
5163
var command = dbConn.GetDialectProvider().ToAlterColumnStatement(modelType, fieldDef);
5264
dbConn.ExecuteSql(command);
5365
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using NUnit.Framework;
2+
using ServiceStack.Logging;
3+
4+
namespace ServiceStack.OrmLite.Tests.Issues
5+
{
6+
public class NullReferenceIssues : OrmLiteTestBase
7+
{
8+
public class Foo
9+
{
10+
public int Id { get; set; }
11+
12+
public string Name { get; set; }
13+
14+
public string Key { get; set; }
15+
}
16+
17+
[Test]
18+
public void Can_AlterColumn()
19+
{
20+
if (Dialect == Dialect.Sqlite)
21+
return; // Not supported
22+
23+
LogManager.LogFactory = new ConsoleLogFactory(debugEnabled:true);
24+
25+
using (var db = OpenDbConnection())
26+
{
27+
db.DropAndCreateTable<Foo>();
28+
29+
db.AlterColumn(typeof(Foo), new FieldDefinition
30+
{
31+
Name = "Name",
32+
FieldType = typeof(string),
33+
IsNullable = true,
34+
DefaultValue = null
35+
});
36+
}
37+
}
38+
39+
}
40+
}

0 commit comments

Comments
 (0)