First: I created a Data-First-Model!
i have a m-to-m relation. To handle this relation i added an entity between:
I want to update all PlayerLeagues (add/delete).
This is my code for this so far:
using (BettingLeagueEntities entities = new BettingLeagueEntities())
{
foreach (PlayerCheckBoxList p in this.PlayerList)
{
PlayerLeague pl = new PlayerLeague();
pl.League = this.ActiveLeague;
pl.Player = p.ActivePlayer;
entities.PlayerLeague.Add(pl);
}
entities.SaveChanges();
}
Player class:
public partial class Player
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Player()
{
this.Bet = new HashSet<Bet>();
this.PlayerLeague = new HashSet<PlayerLeague>();
}
public int PID { get; set; }
public string NickName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string BettingSite { get; set; }
public Nullable<double> Balance { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Bet> Bet { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PlayerLeague> PlayerLeague { get; set; }
}
League class:
public partial class League
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public League()
{
this.Bet = new HashSet<Bet>();
this.PlayerLeague = new HashSet<PlayerLeague>();
}
public int LID { get; set; }
public string Name { get; set; }
public Nullable<System.DateTime> StartDate { get; set; }
public Nullable<System.DateTime> EndDate { get; set; }
public string Path { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Bet> Bet { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PlayerLeague> PlayerLeague { get; set; }
}
PlayerLeague:
public partial class PlayerLeague
{
public int PLID { get; set; }
public int PID { get; set; }
public int LID { get; set; }
public virtual League League { get; set; }
public virtual Player Player { get; set; }
}
It does somehow work, but it creates duplicate entries and i can't do this twice or delete an entry.
I hope you can help me!
Edit:
This does not add multiple entries:
using (BettingLeagueEntities entities = new BettingLeagueEntities())
{
foreach (PlayerCheckBoxList p in this.PlayerList)
{
if(p.IsSelected == true)
{
PlayerLeague pl = new PlayerLeague();
pl.League = this.ActiveLeague;
pl.Player = p.ActivePlayer;
entities.Entry(p.ActivePlayer).State = System.Data.Entity.EntityState.Modified;
entities.Entry(this.ActiveLeague).State = System.Data.Entity.EntityState.Modified;
p.ActivePlayer.PlayerLeague.Add(pl);
this.ActiveLeague.PlayerLeague.Add(pl);
}
}
entities.SaveChanges();
}
p.ActivePlayer.PlayerLeague.Add(pl);
this.ActiveLeague.PlayerLeague.Add(pl);
}
}
But i get this exception:
Additional information: Attaching an entity of type 'BettingLeague.Model.PlayerLeague' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
