Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SeedData #9772

Merged
merged 2 commits into from
Sep 13, 2017
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
77 changes: 77 additions & 0 deletions src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ protected virtual void GenerateEntityType(

if (builderName.StartsWith("b", StringComparison.Ordinal))
{
// ReSharper disable once InlineOutVariableDeclaration
var counter = 1;
if (builderName.Length > 1
&& int.TryParse(builderName.Substring(1, builderName.Length - 1), out counter))
Expand Down Expand Up @@ -191,6 +192,8 @@ protected virtual void GenerateEntityType(
{
GenerateRelationships(builderName, entityType, stringBuilder);
}

GenerateSeedData(entityType.GetProperties(), entityType.GetSeedData(), stringBuilder);
}

stringBuilder
Expand Down Expand Up @@ -938,5 +941,79 @@ protected virtual void GenerateAnnotation(
.Append(Code.UnknownLiteral(annotation.Value))
.Append(")");
}

/// <summary>
/// Generates code for data seeding.
/// </summary>
/// <param name="properties"> The properties to generate. </param>
/// <param name="data"> The data to be seeded. </param>
/// <param name="stringBuilder"> The builder code is added to. </param>
protected virtual void GenerateSeedData(
[NotNull] IEnumerable<IProperty> properties,
[NotNull] IEnumerable<IDictionary<string, object>> data,
[NotNull] IndentedStringBuilder stringBuilder)
{
Check.NotNull(properties, nameof(properties));
Check.NotNull(data, nameof(data));
Check.NotNull(stringBuilder, nameof(stringBuilder));

var dataList = data.ToList();
if (dataList.Count == 0)
{
return;
}

var propertiesToOutput = properties.ToList();

stringBuilder
.AppendLine()
.AppendLine($"b.{nameof(EntityTypeBuilder.SeedData)}(new[]")
.AppendLine("{");

using (stringBuilder.Indent())
{
var firstDatum = true;
foreach (var o in dataList)
{
if (!firstDatum)
{
stringBuilder.AppendLine(",");
}
else
{
firstDatum = false;
}

stringBuilder.Append("new { ");

var firstProperty = true;
foreach (var property in propertiesToOutput)
{
if (o.TryGetValue(property.Name, out var value) && value != null)
{
if (!firstProperty)
{
stringBuilder.Append(", ");
}
else
{
firstProperty = false;
}

stringBuilder
.Append(Code.Identifier(property.Name))
.Append(" = ")
.Append(Code.UnknownLiteral(value));
}
}

stringBuilder.Append(" }");
}
}

stringBuilder
.AppendLine()
.AppendLine("});");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ public virtual void UpdateDataOperation_composite_key()
Values = new object[,]
{
{ "Hodor" },
{ "Homeless Harry" }
{ "Harry" }
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public static readonly IDictionary<Type, ServiceCharacteristics> RelationalServi
{ typeof(IMigrationCommandExecutor), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRelationalCommandBuilderFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRawSqlCommandBuilder), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMigrationsModelDiffer), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMigrationsSqlGenerator), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRelationalTypeMapper), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRelationalValueBufferFactoryFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
Expand All @@ -80,6 +79,7 @@ public static readonly IDictionary<Type, ServiceCharacteristics> RelationalServi
{ typeof(IQuerySqlGeneratorFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ICommandBatchPreparer), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IModificationCommandBatchFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrationsModelDiffer), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrationsAssembly), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IBatchExecutor), new ServiceCharacteristics(ServiceLifetime.Scoped) },
Expand Down
Loading