문제

I am using the EF toolkit to create my POCO classes from an existing database. As the project proceeds I will have to generate this classes several times. The generated classes have some problems I do not want to have to correct every time generate the code.

Problem 1:

It does not generate good property names when a table has tow foreign keys to the same table. Example:

It generates:

public partial class Flow
    {
        public virtual Element Element { get; set; }
        public virtual Element Element1 { get; set; }
    }

when I wanted:

public partial class Flow
    {
        public virtual Element FromElement { get; set; }
        public virtual Element ToElement { get; set; }
    }

This I solved by using partial classes like that:

public partial class Flow
{
    public virtual Element FromElement 
    {
        get {return Element;}
        set { Element = value; }
    }
    public virtual Element ToElement 
    {
        get {return Element1;}
        set {Element1=value;}
    }
}

This way I do not have to edit the generated classes.

Second Problem:

it does not recognize auto increment GUID properties on the database. It generates:

 public partial class Process
     {
         public System.Guid ProcessId { get; set; }
     }

When it should generate:

public partial class Process
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public System.Guid ProcessId { get; set; }
    }

Question is, How to I tell EF that these columns are autoincrement without having to insert "[DatabaseGenerated(DatabaseGeneratedOption.Identity)]" before each primary key field on the generated classes?

도움이 되었습니까?

해결책

Actually, I found out it's easy to corect the T4 templates for the generated code. Just edit Mapping.tt and inserted the last line in the code below.:

// Primary Key
<#
    if (efHost.EntityType.KeyMembers.Count() == 1)
    {
#>
            this.HasKey(t => t.<#= efHost.EntityType.KeyMembers.Single().Name #>)
            .Property(t => t.<#= efHost.EntityType.KeyMembers.Single().Name #>).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top