diff --git a/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs b/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs index ad837307..7b034f67 100644 --- a/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs +++ b/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs @@ -154,6 +154,7 @@ private static void MapEntity(EntityClassOptions option, EntityClass entity) option.EntityNaming = entity.EntityNaming; option.RelationshipNaming = entity.RelationshipNaming; option.PrefixWithSchemaName = entity.PrefixWithSchemaName; + option.RelationshipAttributes = entity.RelationshipAttributes; MapSelection(option.Renaming, entity.Renaming); } diff --git a/src/EntityFrameworkCore.Generator.Core/Options/EntityClassOptions.cs b/src/EntityFrameworkCore.Generator.Core/Options/EntityClassOptions.cs index 3878ce7a..05d0dc40 100644 --- a/src/EntityFrameworkCore.Generator.Core/Options/EntityClassOptions.cs +++ b/src/EntityFrameworkCore.Generator.Core/Options/EntityClassOptions.cs @@ -79,4 +79,9 @@ public string BaseClass /// The renaming expressions. /// public SelectionOptions Renaming { get; } + + /// + /// Attributes which are added to the generated relationships, ;-separated + /// + public string RelationshipAttributes { get; set; } } diff --git a/src/EntityFrameworkCore.Generator.Core/Serialization/EntityClass.cs b/src/EntityFrameworkCore.Generator.Core/Serialization/EntityClass.cs index 44df5285..f737cd9c 100644 --- a/src/EntityFrameworkCore.Generator.Core/Serialization/EntityClass.cs +++ b/src/EntityFrameworkCore.Generator.Core/Serialization/EntityClass.cs @@ -70,4 +70,9 @@ public EntityClass() /// The renaming expressions. /// public SelectionModel Renaming { get; set; } + + /// + /// Attributes which are added to the generated relationships, ;-separated + /// + public string RelationshipAttributes { get; set; } } diff --git a/src/EntityFrameworkCore.Generator.Core/Templates/EntityClassTemplate.cs b/src/EntityFrameworkCore.Generator.Core/Templates/EntityClassTemplate.cs index 8bea59bf..927c166a 100644 --- a/src/EntityFrameworkCore.Generator.Core/Templates/EntityClassTemplate.cs +++ b/src/EntityFrameworkCore.Generator.Core/Templates/EntityClassTemplate.cs @@ -177,6 +177,14 @@ private void GenerateRelationshipProperties() CodeBuilder.AppendLine("/// "); } + if (!Options.Data.Entity.RelationshipAttributes.IsNullOrWhiteSpace()) + { + var splittedAttributes = Options.Data.Entity.RelationshipAttributes.Split(';').Distinct() + .Where(s => !s.IsNullOrWhiteSpace()); + + foreach (var attribute in splittedAttributes) + CodeBuilder.AppendLine($"[{attribute}]"); + } CodeBuilder.AppendLine($"public virtual ICollection<{primaryFullName}> {propertyName} {{ get; set; }}"); CodeBuilder.AppendLine(); @@ -196,6 +204,15 @@ private void GenerateRelationshipProperties() CodeBuilder.AppendLine($"/// "); } + if (!Options.Data.Entity.RelationshipAttributes.IsNullOrWhiteSpace()) + { + var splittedAttributes = Options.Data.Entity.RelationshipAttributes.Split(';').Distinct() + .Where(s => !s.IsNullOrWhiteSpace()); + + foreach (var attribute in splittedAttributes) + CodeBuilder.AppendLine($"[{attribute}]"); + } + if (!Options.Project.Nullable) CodeBuilder.AppendLine($"public virtual {primaryFullName} {propertyName} {{ get; set; }}"); else if (relationship.Cardinality == Cardinality.One) diff --git a/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml b/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml index 5d253569..ce2c37b3 100644 --- a/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml +++ b/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml @@ -34,6 +34,7 @@ data: relationshipNaming: Plural document: false prefixWithSchemaName: false + relationshipAttributes: '' renaming: entities: - ^(sp|tbl|udf|vw)_