0

First of all, I followed the example from this link: http://blog.bennymichielsen.be/2011/06/02/entity-framework-4-1-one-to-one-mapping/ which seems solid, but when I try to create the scaffold, it crashes giving me the error that I have no navigation key.

Here's my code:

The class in charge:

 public class CodigoAgrupadorCuentas 
 {
    public CodigoAgrupadorCuentas()
    {
        CatalogoDeCuentas = new CatalogoDeCuentas();
    }

    public int CodigoAgrupadorCuentasID { get; set; }

    public string description { get; set; }

    public virtual CatalogoDeCuentas CatalogoDeCuentas { get; set; }

}

The dependant class:

public class CatalogoDeCuentas       
 {

    [ForeignKey("CodigoAgrupadorCuentasID")]
    public int CodigoAgrupadorCuentasID { get; set; }

    public string text { get; set; }

}

The DbContext class:

public class PolizasDBContext: DbContext
{
      public PolizasDBContext()
        : base("PolizasDBContext")
    { 

    }

      public DbSet<CatalogoDeCuentas> TablaCatalogoDeCuentas { get; set; }
      public DbSet<CodigoAgrupadorCuentas> TablaCodigoAgrupCuentas { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<CodigoAgrupadorCuentas>()
            .HasRequired(x => x.CatalogoDeCuentas)
            .WithRequiredPrincipal();
        base.OnModelCreating(modelBuilder);

    }

}

And finally, here's the error image I get when I try to scaffold CatalogoDeCuentas:

Scaffolding Error

To summarize, What I want is to be able to Create a One-to-One Relationship where I can insert COdigoAgrupadorCuentas by itself, but not CatalogoDeCuentas.

I've been stuck at this simple problem for hours, any help is appreciated.

Jorge Díaz
  • 137
  • 1
  • 1
  • 10

2 Answers2

0

Hi i think you need to specify the attribute [ForeignKey("CodigoAgrupadorCuentasID")] above the public virtual CuentasDelMes CuentasDelMes { get; set; }

like this:

public class CatalogoDeCuentas       
{
   public int CodigoAgrupadorCuentasID { get; set; }
   public string text { get; set; }

   [ForeignKey("CodigoAgrupadorCuentasID")]
   public virtual CuentasDelMes CuentasDelMes { get; set; }
}
Jordy van Eijk
  • 2,718
  • 2
  • 19
  • 37
  • Hi Jordy, I apologize, I made a mistake and forgot to remove CuentaDelMes from the code I posted, that's another relationship that the entity has and I already tested it earlier. I have updated my post removing that line of code. My code is completely based off the end of the link that I provided, if you want to check the example out. I already had tried other methods before that too but there were always problems when trying to insert data to the table. – Jorge Díaz Sep 23 '14 at 16:50
  • @JorgeDíaz the problem still there but now on the other class, i think look at the first two line in my answer – Jordy van Eijk Sep 23 '14 at 16:55
0

After doing more reseach, I found the answer to my problems. Here's how the code ended:

The class in charge:

public class CodigoAgrupadorCuentas
{
    public int CodigoAgrupadorCuentasID { get; set; }
    public string text { get; set;}

    public virtual CatalogoDeCuentas CatalogoDeCuentas { get; set; }

}

Dependent class:

public class CatalogoDeCuentas  
{

    public int CodigoAgrupadorCuentasID { get; set; }
    public string description { get; set; }

    public virtual CodigoAgrupadorCuentas CodigoAgrupadorCuentas { get; set; }

}

Context Class:

   public class PolizasDBContext: DbContext
{
      public PolizasDBContext()
        : base("PolizasDBContext")
    { 

    }

      public DbSet<CatalogoDeCuentas> TablaCatalogoDeCuentas { get; set; }
      public DbSet<CodigoAgrupadorCuentas> TablaCodigoAgrupCuentas { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<CodigoAgrupadorCuentas>()
            .HasOptional(x => x.CatalogoDeCuentas)
            .WithRequired(x => x.CodigoAgrupadorCuentas);
        base.OnModelCreating(modelBuilder);

    }

}

I got the answer from the following stack overflow question: Entity Framework Code First : Setting up One-To-One foreign key association using Annotations

Thanks a lot anyone that read the question.

Community
  • 1
  • 1
Jorge Díaz
  • 137
  • 1
  • 1
  • 10