简体   繁体   中英

How to configure One-to-One Relationship in Entity Framework, Code First

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:

脚手架错误

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.

Hi i think you need to specify the attribute [ForeignKey("CodigoAgrupadorCuentasID")] above the public virtual CuentasDelMes CuentasDelMes { get; set; } 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; }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM