Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
https://github.com/msawczyn/EFDesigner/issues/129
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSawczyn-AWH committed Dec 3, 2019
1 parent 7f307b5 commit 3af4e54
Show file tree
Hide file tree
Showing 23 changed files with 650 additions and 223 deletions.
39 changes: 18 additions & 21 deletions src/DslPackage/TextTemplates/EF6Designer.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -347,27 +347,6 @@ private void WriteModelCreateEF6(ModelRoot modelRoot, ModelClass[] classesWithTa
if (modelClass.IsDependentType)
continue;

// indexed properties
foreach (ModelAttribute indexed in modelClass.Attributes.Where(x => x.Indexed && !x.IsIdentity))
{
segments.Clear();

segments.Add(indexed.AutoProperty
? $"modelBuilder.Entity<{modelClass.FullName}>().HasIndex(t => t.{indexed.Name})"
: $"modelBuilder.Entity<{modelClass.FullName}>().HasIndex(\"_{indexed.Name}\")");

if (indexed.IndexedUnique)
segments.Add("IsUnique()");

if (segments.Count > 1)
{
if (modelRoot.ChopMethodChains)
OutputChopped(segments);
else
Output(string.Join(".", segments) + ";");
}
}

// attribute level
foreach (ModelAttribute modelAttribute in modelClass.Attributes.Where(x => x.Persistent && !SpatialTypes.Contains(x.Type)))
{
Expand Down Expand Up @@ -408,6 +387,24 @@ private void WriteModelCreateEF6(ModelRoot modelRoot, ModelClass[] classesWithTa
else
Output(string.Join(".", segments) + ";");
}

if (modelAttribute.Indexed && !modelAttribute.IsIdentity)
{
segments.Clear();

segments.Add($"modelBuilder.Entity<{modelClass.FullName}>().HasIndex(t => t.{modelAttribute.Name})");

if (modelAttribute.IndexedUnique)
segments.Add("IsUnique()");

if (segments.Count > 1)
{
if (modelRoot.ChopMethodChains)
OutputChopped(segments);
else
Output(string.Join(".", segments) + ";");
}
}
}

if (!isDependent)
Expand Down
32 changes: 15 additions & 17 deletions src/DslPackage/TextTemplates/EFCoreDesigner.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -297,23 +297,6 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments
if (modelClass.IsDependentType)
continue;

// indexed properties
foreach (ModelAttribute indexed in modelClass.Attributes.Where(x => x.Indexed && !x.IsIdentity))
{
segments.Clear();
segments.Add(indexed.AutoProperty
? $"modelBuilder.Entity<{modelClass.FullName}>().HasIndex(t => t.{indexed.Name})"
: $"modelBuilder.Entity<{modelClass.FullName}>().HasIndex(\"_{indexed.Name}\")");

if (indexed.IndexedUnique)
segments.Add("IsUnique()");

if (modelRoot.ChopMethodChains)
OutputChopped(segments);
else
Output(string.Join(".", segments) + ";");
}

// attribute level
foreach (ModelAttribute modelAttribute in modelClass.Attributes.Where(x => x.Persistent && !SpatialTypes.Contains(x.Type)))
{
Expand Down Expand Up @@ -362,6 +345,21 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments
else
Output(string.Join(".", segments) + ";");
}

if (modelAttribute.Indexed && !modelAttribute.IsIdentity)
{
segments.Clear();

segments.Add($"modelBuilder.Entity<{modelClass.FullName}>().HasIndex(t => t.{modelAttribute.Name})");

if (modelAttribute.IndexedUnique)
segments.Add("IsUnique()");

if (modelRoot.ChopMethodChains)
OutputChopped(segments);
else
Output(string.Join(".", segments) + ";");
}
}

bool hasDefinedConcurrencyToken = modelClass.AllAttributes.Any(x => x.IsConcurrencyToken);
Expand Down
3 changes: 2 additions & 1 deletion src/DslPackage/TextTemplates/EFDesigner.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -909,10 +909,11 @@ void WriteProperties(ModelClass modelClass)

if (!modelAttribute.IsConcurrencyToken && !modelAttribute.AutoProperty)
{
string visibility = modelAttribute.Indexed ? "internal" : "protected";
Output("/// <summary>");
Output($"/// Backing field for {modelAttribute.Name}");
Output("/// </summary>");
Output($"protected {modelAttribute.FQPrimitiveType}{nullable} _{modelAttribute.Name};");
Output($"{visibility} {modelAttribute.FQPrimitiveType}{nullable} _{modelAttribute.Name};");
Output("/// <summary>");
Output($"/// When provided in a partial class, allows value of {modelAttribute.Name} to be changed before setting.");
Output("/// </summary>");
Expand Down
144 changes: 71 additions & 73 deletions src/DslPackage/TextTemplates/EditingOnly/EF6Designer.cs

Large diffs are not rendered by default.

108 changes: 54 additions & 54 deletions src/DslPackage/TextTemplates/EditingOnly/EFCoreDesigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void GenerateEFCore(Manager manager, ModelRoot modelRoot)
// DatabaseInitializer not yet supported in EF Core
// manager.StartNewFile(Path.Combine(modelRoot.ContextOutputDirectory, $"{modelRoot.EntityContainerName}DatabaseInitializer.{modelRoot.FileNameMarker}.cs"));
// WriteDatabaseInitializerEFCore(modelRoot);

// MigrationConfiguration not yet supported in EF Core
// manager.StartNewFile(Path.Combine(modelRoot.ContextOutputDirectory, $"{modelRoot.EntityContainerName}DbMigrationConfiguration.{modelRoot.FileNameMarker}.cs"));
// WriteMigrationConfigurationEFCore(modelRoot);
Expand Down Expand Up @@ -186,8 +186,8 @@ private void WriteConstructorsEFCore(ModelRoot modelRoot)
if (!string.IsNullOrEmpty(modelRoot.ConnectionString) || !string.IsNullOrEmpty(modelRoot.ConnectionStringName))
{
string connectionString = string.IsNullOrEmpty(modelRoot.ConnectionString)
? $"Name={modelRoot.ConnectionStringName}"
: modelRoot.ConnectionString;
? $"Name={modelRoot.ConnectionStringName}"
: modelRoot.ConnectionString;

Output("/// <summary>");
Output("/// Default connection string");
Expand Down Expand Up @@ -273,8 +273,8 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments
if (classesWithTables.Contains(modelClass))
{
segments.Add(modelClass.DatabaseSchema == modelClass.ModelRoot.DatabaseSchema
? $"ToTable(\"{modelClass.TableName}\")"
: $"ToTable(\"{modelClass.TableName}\", \"{modelClass.DatabaseSchema}\")");
? $"ToTable(\"{modelClass.TableName}\")"
: $"ToTable(\"{modelClass.TableName}\", \"{modelClass.DatabaseSchema}\")");

// primary key code segments must be output last, since HasKey returns a different type
List<ModelAttribute> identityAttributes = modelClass.IdentityAttributes.ToList();
Expand All @@ -297,21 +297,6 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments
if (modelClass.IsDependentType)
continue;

// indexed properties
foreach (ModelAttribute indexed in modelClass.Attributes.Where(x => x.Indexed && !x.IsIdentity))
{
segments.Clear();
segments.Add($"modelBuilder.Entity<{modelClass.FullName}>().HasIndex(t => t.{indexed.Name})");

if (indexed.IndexedUnique)
segments.Add("IsUnique()");

if (modelRoot.ChopMethodChains)
OutputChopped(segments);
else
Output(string.Join(".", segments) + ";");
}

// attribute level
foreach (ModelAttribute modelAttribute in modelClass.Attributes.Where(x => x.Persistent && !SpatialTypes.Contains(x.Type)))
{
Expand Down Expand Up @@ -346,8 +331,8 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments
if (modelAttribute.IsIdentity)
{
segments.Add(modelAttribute.IdentityType == IdentityType.AutoGenerated
? "ValueGeneratedOnAdd()"
: "ValueGeneratedNever()");
? "ValueGeneratedOnAdd()"
: "ValueGeneratedNever()");
}

if (segments.Any())
Expand All @@ -360,6 +345,21 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments
else
Output(string.Join(".", segments) + ";");
}

if (modelAttribute.Indexed && !modelAttribute.IsIdentity)
{
segments.Clear();

segments.Add($"modelBuilder.Entity<{modelClass.FullName}>().HasIndex(t => t.{modelAttribute.Name})");

if (modelAttribute.IndexedUnique)
segments.Add("IsUnique()");

if (modelRoot.ChopMethodChains)
OutputChopped(segments);
else
Output(string.Join(".", segments) + ";");
}
}

bool hasDefinedConcurrencyToken = modelClass.AllAttributes.Any(x => x.IsConcurrencyToken);
Expand All @@ -377,8 +377,8 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments
// navigation properties
// ReSharper disable once LoopCanBePartlyConvertedToQuery
foreach (UnidirectionalAssociation association in Association.GetLinksToTargets(modelClass)
.OfType<UnidirectionalAssociation>()
.Where(x => x.Persistent && !x.Target.IsDependentType))
.OfType<UnidirectionalAssociation>()
.Where(x => x.Persistent && !x.Target.IsDependentType))
{
if (visited.Contains(association))
continue;
Expand Down Expand Up @@ -410,14 +410,14 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments

break;

//case Sawczyn.EFDesigner.EFModel.Multiplicity.OneMany:
// segments.Add($"HasMany(x => x.{association.TargetPropertyName})");
// break;
//case Sawczyn.EFDesigner.EFModel.Multiplicity.OneMany:
// segments.Add($"HasMany(x => x.{association.TargetPropertyName})");
// break;
}

string columnPrefix = association.SourceRole == EndpointRole.Dependent
? ""
: association.Target.Name + "_";
? ""
: association.Target.Name + "_";

switch (association.SourceMultiplicity) // realized by shadow property on target
{
Expand All @@ -437,8 +437,8 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments
segments.Add("WithOne()");

segments.Add(association.TargetMultiplicity != Sawczyn.EFDesigner.EFModel.Multiplicity.ZeroMany
? $"HasForeignKey<{association.Source.FullName}>(\"{columnPrefix}{association.TargetPropertyName}_Id\")"
: $"HasForeignKey(\"{columnPrefix}{association.TargetPropertyName}_Id\")");
? $"HasForeignKey<{association.Source.FullName}>(\"{columnPrefix}{association.TargetPropertyName}_Id\")"
: $"HasForeignKey(\"{columnPrefix}{association.TargetPropertyName}_Id\")");

required = true;

Expand All @@ -448,14 +448,14 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments
segments.Add("WithOne()");

segments.Add(association.TargetMultiplicity != Sawczyn.EFDesigner.EFModel.Multiplicity.ZeroMany
? $"HasForeignKey<{association.Source.FullName}>(\"{columnPrefix}{association.TargetPropertyName}_Id\")"
: $"HasForeignKey(\"{columnPrefix}{association.TargetPropertyName}_Id\")");
? $"HasForeignKey<{association.Source.FullName}>(\"{columnPrefix}{association.TargetPropertyName}_Id\")"
: $"HasForeignKey(\"{columnPrefix}{association.TargetPropertyName}_Id\")");

break;

//case Sawczyn.EFDesigner.EFModel.Multiplicity.OneMany:
// segments.Add("HasMany()");
// break;
//case Sawczyn.EFDesigner.EFModel.Multiplicity.OneMany:
// segments.Add("HasMany()");
// break;
}

if (required)
Expand All @@ -464,8 +464,8 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments
if (association.TargetRole == EndpointRole.Principal || association.SourceRole == EndpointRole.Principal)
{
DeleteAction deleteAction = association.SourceRole == EndpointRole.Principal
? association.SourceDeleteAction
: association.TargetDeleteAction;
? association.SourceDeleteAction
: association.TargetDeleteAction;

switch (deleteAction)
{
Expand All @@ -488,8 +488,8 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments
}

foreach (UnidirectionalAssociation association in Association.GetLinksToTargets(modelClass)
.OfType<UnidirectionalAssociation>()
.Where(x => x.Persistent && x.Target.IsDependentType))
.OfType<UnidirectionalAssociation>()
.Where(x => x.Persistent && x.Target.IsDependentType))
{
if (association.TargetMultiplicity == Sawczyn.EFDesigner.EFModel.Multiplicity.ZeroOne || association.TargetMultiplicity == Sawczyn.EFDesigner.EFModel.Multiplicity.One)
Output($"modelBuilder.Entity<{modelClass.FullName}>().OwnsOne(x => x.{association.TargetPropertyName});");
Expand Down Expand Up @@ -534,9 +534,9 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments

break;

//case Sawczyn.EFDesigner.EFModel.Multiplicity.OneMany:
// segments.Add($"HasMany(x => x.{association.SourcePropertyName})");
// break;
//case Sawczyn.EFDesigner.EFModel.Multiplicity.OneMany:
// segments.Add($"HasMany(x => x.{association.SourcePropertyName})");
// break;
}

switch (association.TargetMultiplicity) // realized by property on source
Expand All @@ -561,14 +561,14 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments

break;

//case Sawczyn.EFDesigner.EFModel.Multiplicity.OneMany:
// segments.Add($"HasMany(x => x.{association.TargetPropertyName})");
// break;
//case Sawczyn.EFDesigner.EFModel.Multiplicity.OneMany:
// segments.Add($"HasMany(x => x.{association.TargetPropertyName})");
// break;
}

string foreignKeySegment = CreateForeignKeyColumnSegmentEFCore(association
, foreignKeyColumns
, association.SourceMultiplicity != Sawczyn.EFDesigner.EFModel.Multiplicity.ZeroMany && association.TargetMultiplicity != Sawczyn.EFDesigner.EFModel.Multiplicity.ZeroMany);
, foreignKeyColumns
, association.SourceMultiplicity != Sawczyn.EFDesigner.EFModel.Multiplicity.ZeroMany && association.TargetMultiplicity != Sawczyn.EFDesigner.EFModel.Multiplicity.ZeroMany);

if (foreignKeySegment != null)
segments.Add(foreignKeySegment);
Expand All @@ -579,8 +579,8 @@ private void WriteOnModelCreateEFCore(ModelRoot modelRoot, List<string> segments
if (association.TargetRole == EndpointRole.Principal || association.SourceRole == EndpointRole.Principal)
{
DeleteAction deleteAction = association.SourceRole == EndpointRole.Principal
? association.SourceDeleteAction
: association.TargetDeleteAction;
? association.SourceDeleteAction
: association.TargetDeleteAction;

switch (deleteAction)
{
Expand Down Expand Up @@ -624,8 +624,8 @@ string CreateForeignKeyColumnSegmentEFCore(Association association, List<string>
else if (association.TargetRole == EndpointRole.Dependent)
{
nameBase = association is BidirectionalAssociation b
? b.SourcePropertyName
: $"{association.Source.Name}.{association.TargetPropertyName}";
? b.SourcePropertyName
: $"{association.Source.Name}.{association.TargetPropertyName}";

dependentType = association.Target.FullName;
}
Expand All @@ -647,8 +647,8 @@ string CreateForeignKeyColumnSegmentEFCore(Association association, List<string>
foreignKeyColumns.Add(columnName);

return useGeneric
? $"HasForeignKey<{dependentType}>(\"{columnName}\")"
: $"HasForeignKey(\"{columnName}\")";
? $"HasForeignKey<{dependentType}>(\"{columnName}\")"
: $"HasForeignKey(\"{columnName}\")";
}
}
}
Loading

0 comments on commit 3af4e54

Please sign in to comment.