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

DataAnnotations: Inverse Property is not generated #340

Closed
inforithmics opened this issue Oct 27, 2017 · 2 comments
Closed

DataAnnotations: Inverse Property is not generated #340

inforithmics opened this issue Oct 27, 2017 · 2 comments

Comments

@inforithmics
Copy link

inforithmics commented Oct 27, 2017

public partial class Task
{
    ...
     public string SearchCriteria1 { get; set; }
    ...
    [ForeignKey("SearchCriteria1")] public virtual Suchkriterium SearchCriteriaEntity1 { get; set; } 
    ....
}
public partial class SearchCriteria
{
   ...
   [InverseProperty("SearchCriteriaEntity1")] 
   public virtual System.Collections.Generic.ICollection<Task> Task1 { get; set; } 
   ...
}

Above is a pseudoCode Example of InverseProperty the String value is the name of the Property that holds the Entity of Type SearchCriteriaEntity1 in this case.

Normally it isn't necessary to specify it, but if more than two Collection Properties with the Same Type exist it is necessary to specify or else strange bugs occur.

Currently I use ForeignKeyAnnotationsProcessing to Specify the InverseProperty Attributes.

@inforithmics
Copy link
Author

inforithmics commented Oct 28, 2017

I made following change to the EF.Reverse.POCO.Core.ttinclude

Changed Signature of ForeignKeyAnnotationsProcessing
from
Func<Table, Table, string, string[]> ForeignKeyAnnotationsProcessing;
to
Func<ForeignKey, Table, Table, string, string[]> ForeignKeyAnnotationsProcessing;

fixed all compilation errors

and add following code into ForeignKeyAnnotationsProcessing in EF.Reverse.POCO.tt

        var fkColumn = fkTable.Columns.FirstOrDefault(f => f.Name == foreignKey.FkColumn);
        var pkColumn = pkTable.Columns.FirstOrDefault(f => f.Name == foreignKey.PkColumn);

        if (pkColumn != null && pkColumn.IsPrimaryKey && fkColumn.EntityFk.Count > 0)
        {
            var entityFk = fkColumn.EntityFk[0];
            var definition = entityFk.Definition;
            var curlyBraketPos = definition.IndexOf("{");
            if (curlyBraketPos > 0)
            {
                var propertyNameStart = definition.LastIndexOf(" ", curlyBraketPos - 2);
                if (propertyNameStart > 0)
                {                    
                    var entityProperty = definition.Substring(propertyNameStart, curlyBraketPos - propertyNameStart);
                    entityProperty = entityProperty.Trim();
                    return new[] {"InverseProperty(\"" + entityProperty + "\")"};
                }
            }
        }

The Code is a bit hacky because in the Column.EntityFk I have to parse the ColumnName out of the Definition a little more semantic would be useful here.

jbaig pushed a commit to jbaig/EntityFramework-Reverse-POCO-Code-First-Generator that referenced this issue Sep 5, 2018
@jbaig jbaig mentioned this issue Sep 5, 2018
@sjh37 sjh37 closed this as completed in #461 Sep 5, 2018
sjh37 added a commit that referenced this issue Sep 5, 2018
@sjh37
Copy link
Owner

sjh37 commented Oct 2, 2018

Released v2.37.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants