@@ -191,19 +191,7 @@ public void Sets_owned_type_keys()
191
191
public void Can_diff_against_older_ownership_model ( Type snapshotType )
192
192
{
193
193
using var context = new OwnershipContext ( ) ;
194
- var differ = context . GetService < IMigrationsModelDiffer > ( ) ;
195
- var snapshot = ( ModelSnapshot ) Activator . CreateInstance ( snapshotType ) ;
196
- var reporter = new TestOperationReporter ( ) ;
197
- var modelRuntimeInitializer =
198
- SqlServerTestHelpers . Instance . CreateContextServices ( ) . GetRequiredService < IModelRuntimeInitializer > ( ) ;
199
- var processor = new SnapshotModelProcessor ( reporter , modelRuntimeInitializer ) ;
200
- var model = processor . Process ( snapshot . Model ) ;
201
-
202
- var differences = differ . GetDifferences (
203
- model . GetRelationalModel ( ) ,
204
- context . GetService < IDesignTimeModel > ( ) . Model . GetRelationalModel ( ) ) ;
205
-
206
- Assert . Empty ( differences ) ;
194
+ AssertSameSnapshot ( snapshotType , context ) ;
207
195
}
208
196
209
197
[ ConditionalTheory ]
@@ -213,17 +201,84 @@ public void Can_diff_against_older_ownership_model(Type snapshotType)
213
201
public void Can_diff_against_older_sequence_model ( Type snapshotType )
214
202
{
215
203
using var context = new SequenceContext ( ) ;
204
+ AssertSameSnapshot ( snapshotType , context ) ;
205
+ }
206
+
207
+ private static void AssertSameSnapshot ( Type snapshotType , DbContext context )
208
+ {
216
209
var differ = context . GetService < IMigrationsModelDiffer > ( ) ;
217
210
var snapshot = ( ModelSnapshot ) Activator . CreateInstance ( snapshotType ) ;
218
211
var reporter = new TestOperationReporter ( ) ;
219
- var setBuilder = SqlServerTestHelpers . Instance . CreateContextServices ( ) . GetRequiredService < IModelRuntimeInitializer > ( ) ;
220
- var processor = new SnapshotModelProcessor ( reporter , setBuilder ) ;
221
- var model = processor . Process ( snapshot . Model ) ;
212
+ var modelRuntimeInitializer =
213
+ SqlServerTestHelpers . Instance . CreateContextServices ( ) . GetRequiredService < IModelRuntimeInitializer > ( ) ;
214
+
215
+ var model = PreprocessModel ( snapshot ) ;
216
+ model = new SnapshotModelProcessor ( reporter , modelRuntimeInitializer ) . Process ( model , resetVersion : true ) ;
217
+ var currentModel = context . GetService < IDesignTimeModel > ( ) . Model ;
222
218
223
219
var differences = differ . GetDifferences (
224
- model . GetRelationalModel ( ) , context . GetService < IDesignTimeModel > ( ) . Model . GetRelationalModel ( ) ) ;
220
+ model . GetRelationalModel ( ) ,
221
+ currentModel . GetRelationalModel ( ) ) ;
225
222
226
223
Assert . Empty ( differences ) ;
224
+
225
+ var generator = CSharpMigrationsGeneratorTest . CreateMigrationsCodeGenerator ( ) ;
226
+
227
+ var oldSnapshotCode = generator . GenerateSnapshot (
228
+ "MyNamespace" ,
229
+ context . GetType ( ) ,
230
+ "MySnapshot" ,
231
+ model ) ;
232
+
233
+ var newSnapshotCode = generator . GenerateSnapshot (
234
+ "MyNamespace" ,
235
+ context . GetType ( ) ,
236
+ "MySnapshot" ,
237
+ currentModel ) ;
238
+
239
+ Assert . Equal ( newSnapshotCode , oldSnapshotCode ) ;
240
+ }
241
+
242
+ private static IModel PreprocessModel ( ModelSnapshot snapshot )
243
+ {
244
+ var model = snapshot . Model ;
245
+ if ( model . FindAnnotation ( RelationalAnnotationNames . MaxIdentifierLength ) == null )
246
+ {
247
+ ( ( Model ) model ) [ RelationalAnnotationNames . MaxIdentifierLength ] = 128 ;
248
+ }
249
+
250
+ foreach ( EntityType entityType in model . GetEntityTypes ( ) )
251
+ {
252
+ var schemaAnnotation = entityType . FindAnnotation ( RelationalAnnotationNames . Schema ) ;
253
+ if ( schemaAnnotation != null
254
+ && schemaAnnotation . Value == null )
255
+ {
256
+ entityType . RemoveAnnotation ( RelationalAnnotationNames . Schema ) ;
257
+ }
258
+
259
+ foreach ( var property in entityType . GetProperties ( ) )
260
+ {
261
+ if ( property . IsForeignKey ( ) )
262
+ {
263
+ if ( property . ValueGenerated != ValueGenerated . Never )
264
+ {
265
+ property . SetValueGenerated ( null , ConfigurationSource . Explicit ) ;
266
+ }
267
+
268
+ if ( property . GetValueGenerationStrategy ( ) != SqlServerValueGenerationStrategy . None )
269
+ {
270
+ property . SetValueGenerationStrategy ( null ) ;
271
+ }
272
+ }
273
+ else if ( property . GetValueGenerationStrategy ( ) is SqlServerValueGenerationStrategy strategy
274
+ && strategy != SqlServerValueGenerationStrategy . None )
275
+ {
276
+ property . SetValueGenerationStrategy ( strategy ) ;
277
+ }
278
+ }
279
+ }
280
+
281
+ return model ;
227
282
}
228
283
229
284
private void AddAnnotations ( IMutableAnnotatable element )
0 commit comments