0

I'm using Entity Framework Core 1 ou "7.0.0-rc1-final". I trying to do a entity self reference but it isn't work. Follow the exemplo:

public class Unidade
{
    [Key]
    public Int32 IdUnidade { get; set; }
    public Int32? IdUnidadePai { get; set; }
    public String Nome { get; set; }
    public ICollection<Unidade> LstFilhos { get; set; }
    public Unidade UnidadePai { get; set; }
}

Follow the mapping:

public UnidadeConfiguration(EntityTypeBuilder<Unidade> paramModelBuilder)
{
    paramModelBuilder.HasKey(x => x.IdUnidade);

    paramModelBuilder.HasMany(x => x.LstFilhos)
            .WithOne(x => x.UnidadePai)
            .HasForeignKey(c => c.IdUnidadePai);

    paramModelBuilder.ToTable("Unidade","Unidade");
}

I have already try this to:

    paramModelBuilder.HasOne(x => x.UnidadePai)
            .WithMany(x => x.LstFilhos)
            .HasForeignKey(x => x.IdUnidade);

And try this to:

paramModelBuilder.HasMany(x => x.LstFilhos)
            .WithOne(x => x.UnidadePai)
            .HasForeignKey("IdUnidade", "IdUnidadePai");

My Database sql:

CREATE TABLE [Unidade].[Unidade](
    [IdUnidade] [int] IDENTITY(1,1) NOT NULL,
    [IdUnidadePai] [int] NULL,
    [Nome] [varchar](100) NOT NULL,
    CONSTRAINT [PK_Unidade] PRIMARY KEY CLUSTERED 
    (
        [IdUnidade] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
    ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING ON
GO

ALTER TABLE [Unidade].[Unidade]  WITH CHECK ADD CONSTRAINT [FK_Unidade_Unidade]
    FOREIGN KEY([IdUnidadePai])
REFERENCES [Unidade].[Unidade] ([IdUnidade])
GO

ALTER TABLE [Unidade].[Unidade] CHECK CONSTRAINT [FK_Unidade_Unidade]
GO

So, when I try to get one element the element, I have some scenarios:

  • I get eternal loop, the always search by one like always by id 1
  • I get nothing
  • I get a exception, with the mensage "Additional information: Invalid column name 'IdUnidade1'. Invalid column name 'IdUnidadePai1'."
  • I get a exception, with mensage "Additional information: Invalid column UnidadePaiIdUnidade"

I really don't know what I do.

1 Answers1

1

I've tried to generate code first classes for the database table, you'have posted. So, that's the result:

    public partial class Unidade
    {
        public int IdUnidade { get; set; }
        public int? IdUnidadePai { get; set; }
        public string Nome { get; set; }

        public virtual Unidade IdUnidadePaiNavigation { get; set; }
        public virtual ICollection<Unidade> InverseIdUnidadePaiNavigation { get; set; }
     }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Unidade>(entity =>
        {
            entity.HasKey(e => e.IdUnidade);

            entity.Property(e => e.Nome)
                .IsRequired()
                .HasMaxLength(100)
                .HasColumnType("varchar");

            entity.HasOne(d => d.IdUnidadePaiNavigation).WithMany(p => p.InverseIdUnidadePaiNavigation).HasForeignKey(d => d.IdUnidadePai);
        });
    }

If you wanna try to self generate these, I've used the following command:

dnx ef dbcontext scaffold "Data Source=.;Initial Catalog=Unidade;Integrated Security=True;MultipleActiveResultSets=True" EntityFramework.MicrosoftSqlServer

Of course, you have to change the connection string name according to your db settings. You can read detailed explanation of what should you do here: Entity Framework 7 DbContext scaffold

Community
  • 1
  • 1
Tanya Petkova
  • 155
  • 1
  • 1
  • 7
  • Thank you, to answer but It's not work when the Unidade doesn't have IdUnidadePai but is father of orthers. Exemplo new Unidade() {IdUnidade = 1, IdUnidadePai = null }; new Unidade() {IdUnidade = 2, IdUnidadePai = 1 }.. When I search by IdUnidade = 1, it should bring InverseIdUnidadePaiNavigation with one element. – Bruno Costa Apr 28 '16 at 14:19