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

Duplicate class and file names for entities and mapping #134

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions docs/ef/entity.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ data:
entityNaming: Singular
relationshipNaming: Plural
prefixWithSchemaName: false
enumMappings:
dbo.Table.Property: My.Namespace.Enum
```

### name
Expand Down Expand Up @@ -94,6 +96,10 @@ Configuration on how to generate relationship property names. Default: `Plural`

Control if class names should be generated with schema name prefixed eg. dbo.MyTable = DboMyTable. Default: `false`

### enumMappings

Dictionary for enum mappings instead of eg. `int` to replace a property use `schema.table.property: namespace.enum`

### document

Include XML documentation for the generated class. Default: `false`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Property : ModelBase
public DbType DataType { get; set; }

public Type SystemType { get; set; }
public string EnumTypeName { get; set; }

public bool? IsNullable { get; set; }

Expand Down
17 changes: 13 additions & 4 deletions src/EntityFrameworkCore.Generator.Core/ModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ private void CreateProperties(Entity entity, IEnumerable<DatabaseColumn> columns
property.StoreType = mapping.StoreType;
property.NativeType = mapping.StoreTypeNameBase;
property.DataType = mapping.DbType ?? DbType.AnsiString;

property.SystemType = mapping.ClrType;
var fullName = $"{column.Table.Schema}.{column.Table.Name}.{column.Name}";
property.EnumTypeName = _options.Data.Entity.EnumMappings.GetValueOrDefault(fullName);
property.Size = mapping.Size;

property.IsProcessed = true;
Expand Down Expand Up @@ -227,10 +230,16 @@ private void CreateRelationships(EntityContext entityContext, Entity entity, Dat

private void CreateRelationship(EntityContext entityContext, Entity foreignEntity, DatabaseForeignKey tableKeySchema)
{
_options.Variables.Set(VariableConstants.TableSchema, ToLegalName(tableKeySchema.PrincipalTable.Schema));
_options.Variables.Set(VariableConstants.TableName, ToLegalName(tableKeySchema.PrincipalTable.Name));

Entity primaryEntity = GetEntity(entityContext, tableKeySchema.PrincipalTable, false, false);

_options.Variables.Set(VariableConstants.TableSchema, ToLegalName(tableKeySchema.Table.Schema));
_options.Variables.Set(VariableConstants.TableName, ToLegalName(tableKeySchema.Table.Name));

string primaryName = primaryEntity.EntityClass;
string foreignName = foreignEntity.EntityClass;
string foreignName = tableKeySchema.Name.ToLower();

string relationshipName = tableKeySchema.Name;
relationshipName = _namer.UniqueRelationshipName(relationshipName);
Expand Down Expand Up @@ -266,9 +275,9 @@ private void CreateRelationship(EntityContext entityContext, Entity foreignEntit
foreignRelationship.Entity = foreignEntity;
foreignRelationship.Properties = new PropertyCollection(foreignMembers);

string prefix = GetMemberPrefix(foreignRelationship, primaryName, foreignName);
//string prefix = GetMemberPrefix(foreignRelationship, primaryName, foreignName);

string foreignPropertyName = ToPropertyName(foreignEntity.EntityClass, prefix + primaryName);
string foreignPropertyName = ToPropertyName(foreignEntity.EntityClass, /*prefix +*/ foreignName);
foreignPropertyName = _namer.UniqueName(foreignEntity.EntityClass, foreignPropertyName);
foreignRelationship.PropertyName = foreignPropertyName;

Expand Down Expand Up @@ -297,7 +306,7 @@ private void CreateRelationship(EntityContext entityContext, Entity foreignEntit
else
primaryRelationship.Cardinality = Cardinality.Many;

string primaryPropertyName = prefix + foreignName;
string primaryPropertyName = /*prefix +*/ foreignName;
if (!isOneToOne)
primaryPropertyName = RelationshipName(primaryPropertyName);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace EntityFrameworkCore.Generator.Options
Expand All @@ -21,6 +22,7 @@ public EntityClassOptions(VariableDictionary variables, string prefix)
RelationshipNaming = RelationshipNaming.Plural;
EntityNaming = EntityNaming.Singular;
PrefixWithSchemaName = false;
EnumMappings = new Dictionary<string, string>();
}

/// <summary>
Expand Down Expand Up @@ -70,5 +72,8 @@ public string BaseClass
/// </summary>
[DefaultValue(false)]
public bool PrefixWithSchemaName { get; set; }


public Dictionary<string, string> EnumMappings { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ private void GenerateProperties()
foreach (var property in _entity.Properties)
{
var propertyType = property.SystemType.ToNullableType(property.IsNullable == true);
if (property.EnumTypeName != null)
propertyType = property.EnumTypeName.ToNullableType(property.IsNullable == true) + (propertyType.Contains("?") ? "?" : "");

var propertyName = property.PropertyName.ToSafeName();

if (Options.Data.Entity.Document)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ private void GenerateProperties()
foreach (var property in _model.Properties)
{
var propertyType = property.SystemType.ToNullableType(property.IsNullable == true);
if (property.EnumTypeName != null)
propertyType = property.EnumTypeName.ToNullableType(property.IsNullable == true) + (propertyType.Contains("?") ? "?" : "");

var propertyName = property.PropertyName.ToSafeName();

if (ShouldDocument())
Expand Down
115 changes: 115 additions & 0 deletions src/EntityFrameworkCore.Generator/generation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
#---------------------------------#
# https://efg.loresoft.com/en/latest/
# project section - Used for shared variables through out the configuration file
#---------------------------------#
project:
# the root namespace for the project
namespace: 'api'
# the root directory for the project
directory: ./

#---------------------------------#
# data section - Used for configuring database connections
#---------------------------------#
database:
# the connection string to the database
connectionString: 'Server=hulk;Initial Catalog=SISTEMAS;Integrated Security=True;MultipleActiveResultSets=True'
# the database provider name. Default: SqlServer
provider: SqlServer

# config name to read the connection string from the user secrets file
connectionName: 'ConnectionStrings:Generator'
# the user secret identifier, can be shared with .net core project
userSecretsId: '984ef0cf-2b22-4fd1-876d-e01489da4c1f'

# tables to include or empty to include all
tables:
- dbo.LOG_SOP10200
# schemas to include or empty to include all
schemas:
# table naming hint for how existing tables are named. Default: Singular
tableNaming: Singular

#---------------------------------#
# data section - controls the generated files for Entity Framework
#---------------------------------#
data:
# data context file configuration
context:
name: 'ContextoDb' # the data context class name
baseClass: DbContext # the data context base class name
namespace: '{Project.Namespace}.Data' # the data context namespace
directory: '{Project.Directory}/Data' # the data context output directory

# how to generate names for the DbSet properties on the data context. Default: Plural
propertyNaming: Suffix
#include XML documentation
document: false

# entity class file configuration
entity:
namespace: '{Project.Namespace}.Data.Entities' # the entity class namespace
directory: '{Project.Directory}/Data/Entities' # the entity class output directory

# how to generate entity class names from the table name. Default: Singular
entityNaming: Preserve
relationshipNaming: Suffix
name: '{Table.Schema}{Table.Name}Entity'
#baseClass: BaseEntity

# how to generate relationship collections names for the entity. Default: Plural
#relationshipNaming: Suffix
#include XML documentation
document: false

# Generate class names with prefixed schema name eg. dbo.MyTable = DboMyTable
# prefixWithSchemaName: true
enumMappings:
dbo.usuario.activo: api.Data.CustomEnums.YesNoEnum
mapping:
namespace: '{Project.Namespace}.Data.Mapping' # the mapping class namespace
directory: '{Project.Directory}/Data/Mapping' # the mapping class output directory
#include XML documentation
document: false
#---------------------------------#
# model section - controls the optional view model generation
#---------------------------------#
model:
# shared options between read, create and update models
shared:
namespace: '{Project.Namespace}.Data.Models' # the model class namespace
directory: '{Project.Directory}/Data/Models' # the mapping class output directory
# regular expression of entities and properties to exclude in all models
exclude:
# list of regular expressions of entity names
entities:

# list of regular expressions of property names, source is Entity.Property
properties:
# update view model class configuration
update:
generate: true # generate update model class files
name: '{Table.Schema}_{Table.Name}Model' # the update model class name
#baseClass: EntityUpdateModel # the update model base class
namespace: '{Project.Namespace}.Data.Models'
directory: '{Project.Directory}/Data/Models'
exclude:
entities: []
properties: []

# AutoMapper class configuration
mapper:
generate: true
name: '{Table.Schema}_{Table.Name}Profile'
baseClass: Profile
namespace: '{Project.Namespace}.Data.AutoMapper'
directory: '{Project.Directory}/Data/AutoMapper'

# FluentValidation class configuration
validator:
generate: true
name: '{Model.Name}Validator'
baseClass: 'AbstractValidator<{Model.Name}>'
namespace: '{Project.Namespace}.Data.ModelValidation'
directory: '{Project.Directory}/Data/ModelValidation'
Loading