Alexandru
Alexandru

Reputation: 336

Entity Framework update entity with one to one relation

I have two entities:

public class Tournament
{
    [Key]
    public Int32 Id { get; set; }
    public string Name { get; set; }

    public virtual TournamentSite Site { get; set; }
}

public class TournamentSite
{
    [Key]        
    public Int32 Id { get; set;}
    public string Name { get; set; }
}

and in DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        modelBuilder.Entity<Tournament>()
                    .HasRequired<TournamentSite>(t => t.Site)
                    .WithRequiredDependent()
                    .Map(p => p.MapKey("IdSite"));

        base.OnModelCreating(modelBuilder);
}

everything is working fine, I mean when I load a tournament it gets the TournamentSite with proper Id and Name the problem is I want to change a TournamentSite for a specific Tournament. I have tried something like this:

var tournament = dbContext.Tournaments.Find(1); // Get tournament with id 1
tournament.Site.Id = 2;

dbContext.SaveChanges();

I expect that in the Tournament table the IdSite field to be 2 now instead of 1, but whatever I do the IdSite field that is used to link Tournament to TournamentSite it's always 1.

Any help is welcome, Thanks.

Upvotes: 1

Views: 3730

Answers (3)

Alexandru
Alexandru

Reputation: 336

I finally manage to do it, I have redone the models like this:

public class Tournament
{
    [Key]
    public Int32 Id { get; set; }
    public string Name { get; set; }

    public Int32 IdSite { get; set; }

    [ForeignKey("IdSite")]
    public virtual TournamentSite Site { get; set; }
}

public class TournamentSite
{
    [Key]        
    public Int32 Id { get; set;}
    public string Name { get; set; }
}

Nothing needs to be done in OnModelCreating the function looks like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     base.OnModelCreating(modelBuilder);
}

and now I can use:

var tournament = dbContext.Tournaments.Find(1);
tournament.IdSite = 2;

dbContext.SaveChanges();

Thanks for help.

Upvotes: 0

Michael Brown
Michael Brown

Reputation: 9153

The other option is to actually map IdSite to your Tournament object like so

public class Tournament
{
    [Key]
    public Int32 Id { get; set; }
    public string Name { get; set; }
    public Int32 TournamentSiteId { get; set;}
    public virtual TournamentSite Site { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        modelBuilder.Entity<Tournament>()
                    .HasRequired<TournamentSite>(t => t.Site)
                    .WithRequiredDependent()
                    .Map(p => p.MapKey("IdSite"));
        modelBuilder.Entity<Tournament>()
                    .Property(p => p.TournamentSiteId)
                    .HasColumnName("IdSite");
        base.OnModelCreating(modelBuilder);
}

Now you should be able to do this

var tournament = dbContext.Tournaments.Find(1); // Get tournament with id 1
tournament.TournamentSiteId = 2;

dbContext.SaveChanges();

et voila!

Upvotes: 1

Richard Deeming
Richard Deeming

Reputation: 31248

Your code is trying to change the ID of the TournamentSite, not the IdSite of the Tournament. You'll need to assign the Site property instead:

var tournament = dbContext.Tournaments.Find(1);
tournament.Site = dbContext.TournamentSites.Find(2);
dbContext.SaveChanges();

Upvotes: 0

Related Questions