Relationship 1 to 1 with Entity Framework

Asked

Viewed 5,492 times

7

I have 2 entities: Equipamento and Databook. A Equipamento can only have one Databook and a Databook is only for a Equipamento. How can I make this relationship with Entity Framework?

Follows the Classes:

public class Equipamento
{
    public int EquipamentoID { get; set; }

    public string Nome { get; set; }
    public string Sigla { get; set; }
    public Databook databook { get; set; }
}

public class Databook
{
    public int DatabookID { get; set; }

    public string Indentificacao { get; set; }   
    public Equipamento equipamento { get; set; }
}

2 answers

7


A one to one mapping is done through the relationship where the key of one is also key of the other. So we will have the key field in the class Todo being the key to the class parte.

public class Todo{
    public virtual Parte ObjParte { get; set; }
}

public class Parte{
    [Key]
    public int TodoID { get; set; }
    public virtual Todo ObjTodo { get; set; }
}

In class Contexto we can make this mapping also with Fluent Api:

public class Contexto {
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Parte>()
            .HasKey(x => x.TodoID)
            .HasRequired(x => x.ObjTodo)
            .WithRequiredDependent(p => p.ObjParte);
    }
}
  • As already stated in @Tiagosilva’s reply, this approach works, but hurts the normalization and conventions of the Entity Framework.

5

Generally, the relationship is made through the information of Foreign Keys (as Foreign Keys).

For annotations with attributes, it would look like this in Equipamento:

public class Equipamento
{
    [Key]
    public int EquipamentoID { get; set; }

    public string Nome { get; set; }
    public string Sigla { get; set; }

    // Conforme comentário do AP, essa propriedade não pode existir, ou o EF se perde.
    //public virtual Databook databook { get; set; } 
}

Note the use of virtual. It is recommended especially when using the Entity Framework with proxies class usage setting. Here, the type property Databook becomes only a navigation property.

A browsing property only signals to the Entitu Framework that a related record can be found in the specified entity. In this case, we are indicating that a Equipamento may (or may not) have a Databook.

And so in Databook:

public class Databook
{       
    [Key]
    public int DatabookID { get; set; }

    [ForeignKey("equipamento")]
    public int EquipamentoID { get; set; }   
    public virtual Equipamento equipamento { get; set; }

    // ou assim:
    // sendo esse caso o necessário para quando se trata de entidades 
    // com chaves compostas.
    //
    //[Key]
    //public int EquipamentoID { get; set; }   
    //[ForeignKey("EequipamentoId")]
    //public virtual Equipamento equipamento { get; set; }

    public string Indentificacao { get; set; }
}
  • I have to set something up in my context?

  • @Janderson, for the context, is basically declaring the properties of the type DbSet<T>.

  • Tiago, thanks! How would look in the views and controllers, can help me?

  • @Janderson, that’s another question. You can even add in this other question the link to this as a reference, but it’s another question.

  • Tiago, I already asked another question about how I would look in the views and controllers link: http://answall.com/questions/38014/related-1-para-para-1-asp-net-mvc-entity-framework

  • 1

    @Tiagosilva, is giving this error: One or more validation errors Were Detected During model Generation: Databook_equipment_source: Multiplicity is not Valid in Role 'Databook_equipment_source' in Relationship 'Databook_equipment'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'. What do I do?

  • 1

    @Janderson/Gypsy, <<--- Does it work? Actually the navigation property public virtual Databook databook { get; set; } in Equipamento is only accepted if the foreign key in Databook linking to Equipamento were the primary key. I assume that I don’t fully understand.

  • 1

    @Janderson, I commented on the property I told you about, in response, to demonstrate what I said in the comments.

  • 1

    @Thiagosilva, that way it won’t be ratio 1 to N ?

  • 1

    @Janderson, no. Equipamento has no connection with another class, and Databook is linked to only one Equipamento (as represented by the properties that make up FK, the EquipamentoID and equipamento).

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.