0

I have been trying to update a project of my with Entity Framework. I have an existing database and model, which after some work matched. With some more work, I managed to get the application to successfully read the data from the database. But when saving new data, the nightmare started.

In my data structure, I have 2 one-to-many relations, one between player and team and one between match and team. I have been trying many configurations (with [Key], foreign key, inverse property attributes) but I either get an error

Trying to cast List to team

or

no column found Match_id no column found Player_id

This is my code:

public class Match
{
    //[Key]
    //[DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string Id { get; set; }
    //[Column("Date")]
    public DateTime Date { get; set; }

    public DateTime EndTime { get; set; }
    [NotMapped]
    public bool Remove { get; set; }

    //[InverseProperty("Match")]
    [ForeignKey("MatchId")]
    public virtual ICollection<Team> Teams { get; set; }

    public int WinningTeamId { get; set; }

    public MatchType MatchType { get; set; }
    public string Source { get; internal set; }

    public Match()
    {
        WinningTeamId = -1;
        this.Teams = new List<Team>();
        this.EndTime = (DateTime)SqlDateTime.MinValue;
    }
}

public class Team
{
    public int TeamId { get; set; }

    [NotMapped]
    public int RatingChange { get; set; } 

    [Key()]
    [Column("PlayerId", Order = 2)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public virtual Player Player { get; set; }

    [Column(Order = 1)]
    [Key()]
    //[DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string MatchId { get; set; }

    [ForeignKey("MatchId")]
    public virtual Match Match { get; set; }

    [NotMapped]
    public int Rating { get; set; }

    public Team()
    {
    }
}

public class Player
{
    public string name { get; set; }
    public int PastatsId { get; set; }
    public string UberId { get; set; }

    //[ForeignKey("PlayerId")]
    //[InverseProperty("Player")]
    public virtual ICollection<Team> Teams { get; set; }

    //[Key()]
    public int Id { get; set; }
}

To save the new matches, I first save the players to prevent conflicts there:

public void Save(IEnumerable<Match> matches)
{
        foreach (var match in matches)
        {
            foreach (var team in match.Teams)
            {
                var entry = Entry(team.Player);

                if (entry.State == EntityState.Detached)
                {
                    var localplayer = Players.Local.FirstOrDefault(x => x.UberId == team.Player.UberId);

                    if (localplayer == null)
                    {
                        this.Players.Add(team.Player);
                        team.Player = entry.Entity;
                    }
                    else
                    {
                        team.Player = localplayer;
                    }
                }
                else
                {
                    Entry(team.Player).State = EntityState.Modified;
                }
            }
        }

        SaveChanges();

        foreach (var match in matches)
        {
            if (Matches.Find(match.Id) != null)
            {
                continue;
            }

            if (Entry(match).State == EntityState.Detached)
            {
                this.Matches.Add(match);
            }
        }

        SaveChanges();
}

How to do this mapping with an existing database and code?

Any insight will be gratefully appreciated.

River
  • 1
  • 2
  • That is a very common design. See [here](https://stackoverflow.com/questions/5559043/entity-framework-code-first-two-foreign-keys-from-same-table) and [here](https://stackoverflow.com/questions/21492051/cannot-automatically-bind-the-navigation-property-players-on-entity-type-data). First thing, be consistent - you are using string Id for match, int Id for player and int TeamId for team. WinningTeamId = -1? That smells, just use null. – Steve Greene Apr 04 '18 at 15:56
  • First link fixed the issue. knew it wasn't much but was stuck. thanks! id's have a reason. – River Apr 04 '18 at 18:28

0 Answers0