Question

I'm using Entity Framework 5 and the UnitOfWork + Repository pattern.

I am trying to create the following entity:

public partial class ViaggioAttivita
{
    public System.Guid Id { get; set; }
    public System.Guid IdViaggio { get; set; }
    public virtual Viaggio Viaggio { get; set; }
}

public partial class Viaggio
{
    public System.Guid Id { get; set; }
    public virtual ICollection<ViaggioAttivita> ViaggiAttivita { get; set; }
}

I noticed that when i create the new ViaggioAttivita entity populating the IdViaggio, when i do

ViaggioAttivita attivita = new ViaggioAttivita();
attivita.IdViaggio = ParentId;
unitOfWork.ViaggiAttivitaRepository.Insert(attivita);

the navigation property attivita.Viaggio does not get updated.

If I directly update the attivita.Viaggio instead of the Id

ViaggioAttivita attivita = new ViaggioAttivita();
attivita.Viaggio = unitOfWork.ViaggiRepository.GetByID(ParentId);
unitOfWork.ViaggiAttivitaRepository.Insert(attivita);

The Viaggio of course get updated, but the IdViaggio key gets updated too.

What am I missing ?

Why am I getting this difference?

I tried calling a .Save() but nothing changes.

It seems that relations only get updated if I manually update the entity, but they don't get updated if I update the key only.

Thank you

Edit 1:

I'm on Sql Server 2008, MVC3, Entity Framework 5 (runtime v4.0.30319 of course). Database First mode. The two tables have the relationship (of course, otherwise it would not populate the Key using the second method).

Edit 2:

I try to past some EDMX information;

<EntityType Name="Viaggio">
    <Key>
      <PropertyRef Name="Id" />
    </Key>
    <Property Name="Id" Type="Guid" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
    <NavigationProperty Name="ViaggiAttivita" Relationship="DatabaseModel.FK_ViaggiAttivita_Viaggi" FromRole="Viaggi" ToRole="ViaggiAttivita" />
  </EntityType>

  <EntityType Name="ViaggioAttivita">
    <Key>
      <PropertyRef Name="Id" />
    </Key>
    <Property Name="Id" Type="Guid" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
    <Property Name="IdViaggio" Type="Guid" Nullable="false" />
    <NavigationProperty Name="Viaggio" Relationship="DatabaseModel.FK_ViaggiAttivita_Viaggi" FromRole="ViaggiAttivita" ToRole="Viaggi" />
  </EntityType>




 <AssociationSet Name="FK_ViaggiAttivita_Viaggi" Association="DatabaseModel.FK_ViaggiAttivita_Viaggi">
      <End Role="Viaggi" EntitySet="Viaggi" />
      <End Role="ViaggiAttivita" EntitySet="ViaggiAttivita" />
    </AssociationSet>
Was it helpful?

Solution

The difference is:

a) Set Foreign Key Only. (Id) If this Entity is in loaded in the cache the Navigation property can be set. If it isnt loaded, then you would need to trigger the loading. You can search on how to or when this is done automatically. See topic lazy loading versus .include

b) Set navigation property with an entity. the navigation property is supported by Foreign key Id field. Now EF can see the Nav property and Its key. It can set the ID with data it already has. No need to load from DB. SO it is set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top