Skip to content

Commit da4cfd5

Browse files
Ticket #9 : Support user defined type
1 parent e0b74e5 commit da4cfd5

27 files changed

Lines changed: 1313 additions & 22 deletions

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<VersionPrefix>2.0.1</VersionPrefix>
3+
<VersionPrefix>2.0.2</VersionPrefix>
44
<Authors>SimpleIdServer</Authors>
55
<Owners>SimpleIdServer</Owners>
66
</PropertyGroup>

samples/EFCore.Cassandra.Samples/FakeDbContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3838
modelBuilder.Entity<CV>()
3939
.ToTable("cvs", "cv")
4040
.HasKey(c => c.Id);
41+
modelBuilder.Entity<ApplicantAddress>()
42+
.ToUserDefinedType("applicant_addr", "cv")
43+
.HasNoKey();
4144
}
4245
}
4346
}

samples/EFCore.Cassandra.Samples/Migrations/20200616081423_InitialCreate.Designer.cs renamed to samples/EFCore.Cassandra.Samples/Migrations/20200917124959_InitialCreate.Designer.cs

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/EFCore.Cassandra.Samples/Migrations/20200616081423_InitialCreate.cs renamed to samples/EFCore.Cassandra.Samples/Migrations/20200917124959_InitialCreate.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Net;
44
using System.Numerics;
55
using Cassandra;
6+
using EFCore.Cassandra.Samples.Models;
67
using Microsoft.EntityFrameworkCore.Migrations;
78

89
namespace EFCore.Cassandra.Samples.Migrations
@@ -40,7 +41,8 @@ protected override void Up(MigrationBuilder migrationBuilder)
4041
LocalTime = table.Column<LocalTime>(nullable: true),
4142
Lst = table.Column<IEnumerable<string>>(nullable: true),
4243
LstInt = table.Column<IEnumerable<int>>(nullable: true),
43-
Dic = table.Column<IDictionary<string, string>>(nullable: true)
44+
Dic = table.Column<IDictionary<string, string>>(nullable: true),
45+
Address = table.Column<ApplicantAddress>(type: "applicant_addr", nullable: true)
4446
},
4547
constraints: table =>
4648
{
@@ -60,6 +62,15 @@ protected override void Up(MigrationBuilder migrationBuilder)
6062
{
6163
table.PrimaryKey("PK_cvs", x => x.Id);
6264
});
65+
66+
migrationBuilder.CreateUserDefinedType(
67+
name: "applicant_addr",
68+
schema: "cv",
69+
columns: table => new
70+
{
71+
City = table.Column<string>(nullable: true),
72+
StreetNumber = table.Column<int>(nullable: false)
73+
});
6374
}
6475

6576
protected override void Down(MigrationBuilder migrationBuilder)
@@ -71,6 +82,10 @@ protected override void Down(MigrationBuilder migrationBuilder)
7182
migrationBuilder.DropTable(
7283
name: "cvs",
7384
schema: "cv");
85+
86+
migrationBuilder.DropUserDefinedType(
87+
name: "applicant_addr",
88+
schema: "cv");
7489
}
7590
}
7691
}

samples/EFCore.Cassandra.Samples/Migrations/FakeDbContextModelSnapshot.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ protected override void BuildModel(ModelBuilder modelBuilder)
9999
b.HasAnnotation("Cassandra:ClusteringOrderByOptions", "[{\"ColumnName\":\"Order\",\"Order\":0}]");
100100
});
101101

102+
modelBuilder.Entity("EFCore.Cassandra.Samples.Models.ApplicantAddress", b =>
103+
{
104+
b.Property<string>("City")
105+
.HasColumnType("text");
106+
107+
b.Property<int>("StreetNumber")
108+
.HasColumnType("int");
109+
110+
b.ToTable("applicant_addr","cv");
111+
112+
b.HasAnnotation("Cassandra:IsUserDefinedType", true);
113+
});
114+
102115
modelBuilder.Entity("EFCore.Cassandra.Samples.Models.CV", b =>
103116
{
104117
b.Property<Guid>("Id")

samples/EFCore.Cassandra.Samples/Models/Applicant.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ public class Applicant
3232
public IList<string> Lst { get; set; }
3333
public IList<int> LstInt { get; set; }
3434
public IDictionary<string, string> Dic { get; set; }
35+
public ApplicantAddress Address { get; set; }
3536
}
3637
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace EFCore.Cassandra.Samples.Models
2+
{
3+
public class ApplicantAddress
4+
{
5+
public int StreetNumber { get; set; }
6+
public string City { get; set; }
7+
}
8+
}

samples/EFCore.Cassandra.Samples/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ private static Applicant BuildApplicant()
9898
Blob = new byte[] { 1, 2 },
9999
LocalDate = new LocalDate(2019, 10, 2),
100100
Ip = IPAddress.Loopback,
101-
LocalTime = new LocalTime(2, 3, 4, 5)
101+
LocalTime = new LocalTime(2, 3, 4, 5),
102+
Address = new ApplicantAddress
103+
{
104+
City = "Brussels",
105+
StreetNumber = 100
106+
}
102107
};
103108
}
104109
}

src/EFCore.Cassandra/CassandraServiceCollectionExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.EntityFrameworkCore.Diagnostics;
1414
using Microsoft.EntityFrameworkCore.Infrastructure;
1515
using Microsoft.EntityFrameworkCore.Migrations;
16+
using Microsoft.EntityFrameworkCore.Migrations.Internal;
1617
using Microsoft.EntityFrameworkCore.Query;
1718
using Microsoft.EntityFrameworkCore.Storage;
1819
using Microsoft.EntityFrameworkCore.Update;
@@ -27,11 +28,13 @@ public static IServiceCollection AddEntityFrameworkCassandra(this IServiceCollec
2728
.TryAdd<IDatabaseProvider, DatabaseProvider<CassandraOptionsExtension>>()
2829
.TryAdd<IRelationalTypeMappingSource, CassandraTypeMappingSource>()
2930
.TryAdd<LoggingDefinitions, CassandraLoggingDefinitions>()
31+
.TryAdd<IMigrationsModelDiffer, CassandraMigrationsModelDiffer>()
3032
// .TryAdd<IMemberTranslator, CassandraCompositeMemberTranslator>()
3133
.TryAdd<IMigrator, CassandraMigrator>()
3234
.TryAdd<ISqlGenerationHelper, CassandraSqlGenerationHelper>()
3335
.TryAdd<IMigrationsSqlGenerator, CassandraMigrationsSqlGenerator>()
3436
.TryAdd<IRelationalDatabaseCreator, CassandraDatabaseCreator>()
37+
.TryAdd<IModelValidator, CassandraModelValidator>()
3538
.TryAdd<IMigrationCommandExecutor, CassandraMigrationCommandExecutor>()
3639
.TryAdd<IModificationCommandBatchFactory, CassandraModificationCommandBatchFactory>()
3740
.TryAdd<IDatabaseCreator, CassandraDatabaseCreator>()

src/EFCore.Cassandra/SharedTypeExtensions.cs renamed to src/EFCore.Cassandra/CassandraSharedTypeExtensions.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Copyright (c) SimpleIdServer. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3-
using System;
43
using System.Collections.Generic;
54
using System.Linq;
65
using System.Linq.Expressions;
76
using System.Reflection;
87

9-
namespace Microsoft.EntityFrameworkCore.Cassandra
8+
namespace System
109
{
11-
public static class SharedTypeExtensions
10+
public static class CassandraSharedTypeExtensions
1211
{
1312
public static Expression UnwrapTypeConversion(this Expression expression, out Type convertedType)
1413
{
@@ -27,6 +26,9 @@ public static Expression UnwrapTypeConversion(this Expression expression, out Ty
2726
return expression;
2827
}
2928

29+
public static bool IsValidEntityType(this Type type)
30+
=> type.GetTypeInfo().IsClass;
31+
3032
public static Type UnwrapNullableType(this Type type) => Nullable.GetUnderlyingType(type) ?? type;
3133

3234
public static bool IsInteger(this Type type)
@@ -162,5 +164,24 @@ public static IEnumerable<Type> GetBaseTypes(this Type type)
162164
type = type.GetTypeInfo().BaseType;
163165
}
164166
}
167+
168+
public static IEnumerable<PropertyInfo> GetPropertiesInHierarchy(this Type type, string name)
169+
{
170+
do
171+
{
172+
var typeInfo = type.GetTypeInfo();
173+
foreach (var propertyInfo in typeInfo.DeclaredProperties)
174+
{
175+
if (propertyInfo.Name.Equals(name, StringComparison.Ordinal)
176+
&& !(propertyInfo.GetMethod ?? propertyInfo.SetMethod).IsStatic)
177+
{
178+
yield return propertyInfo;
179+
}
180+
}
181+
182+
type = typeInfo.BaseType;
183+
}
184+
while (type != null);
185+
}
165186
}
166187
}

0 commit comments

Comments
 (0)