Error with relationship 1 for many, Power Tools does not generate model

1

I want to do one-to-many relationship in EF where one and only Territory has multiple regions, but when I parse the model in Power Tools it does not recognize it.

Region

[Table("Regiao")]
public class Regiao
{
    [Key]
    [DisplayFormat(DataFormatString = "{0:0000}", ApplyFormatInEditMode = true)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long RegiaoID { get; set; }

    [Display(Name = "Região")]
    [Column(TypeName = "varchar")]
    [StringLength(50, ErrorMessage = "O {0} deve ser de pelo menos {2} caracteres.", MinimumLength = 2)]
    [Required(ErrorMessage = "Campo Obrigatório")]
    public string RegiaoDescricao { get; set; }

    [ForeignKey("FKTerritorioID")]
    public long TerritorioID { get; set; }
    public virtual Territorio Territorio { get; set; }
}

Territory

[Table("Territorio")]
public class Territorio
{
    [Key]
    [DisplayFormat(DataFormatString = "{0:0000}", ApplyFormatInEditMode = true)]
    public long TerritorioID { get; set; }

    [Required(ErrorMessage="Campo Obrigatório")]
    public string TerritorioDescricao { get; set; }

    public ICollection<Regiao> Regioes { get; set; }
}

Context

public class DbEmpresaContext : DbContext
{
    public DbEmpresaContext()
        : base("connDBEmpresaModelo")
    {            
    }

    public DbSet<Regiao> Regioes { get; set; }
    public DbSet<Territorio> Territorios { get; set; }
}
    
asked by anonymous 23.09.2015 / 22:32

1 answer

1

I'll make some small changes to your code. Your Territory entity looks like this:

[Table("Territorio")]
    public class Territorio
    {
        [Key]
        [DisplayFormat(DataFormatString = "{0:0000}", ApplyFormatInEditMode = true)]
        public long TerritorioID { get; set; }

        [Required(ErrorMessage = "Campo Obrigatório")]
        public string TerritorioDescricao { get; set; }

        public virtual ICollection<Regiao> Regioes { get; set; }
    }

In this entity, only the virtual property was added to perform the Lazy Loading of Regions .

Your Region entity will look like this:

[Table("Regiao")]
    public class Regiao
    {
        [Key]
        [DisplayFormat(DataFormatString = "{0:0000}", ApplyFormatInEditMode = true)]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long RegiaoID { get; set; }

        [Display(Name = "Região")]
        [Column(TypeName = "varchar")]
        [StringLength(50, ErrorMessage = "O {0} deve ser de pelo menos {2} caracteres.", MinimumLength = 2)]
        [Required(ErrorMessage = "Campo Obrigatório")]
        public string RegiaoDescricao { get; set; }

        public long FKTerritorioID { get; set; }

        [ForeignKey("FKTerritorioID")]
        public virtual Territorio Territorio { get; set; }
    }

In this entity, the name of your foreing key was changed to FKTerritorioID to accompany your Annotation .

Once you've done that, just call your model again.

Create .EDMX Template with EF

To create a template just right-click on your Context class (in the example above it would be DbEmpresaContext ). After that, navigate to Enitty Framework and after, View Entity Data Model, as shown in the image below.

Image Source: Microsoft.

Following this example, your template will look like the image below:

    
24.09.2015 / 00:16