Question

After i finished my SimpleMembership Step on my Application, there is another problem...

namespace Korbball.Models
{
    public class GamesContext : DbContext
    {
        public GamesContext()
            : base("KorbballDBContext")
        {
        }

        public DbSet<Games> Games { get; set; }
        public DbSet<Teams> Teams { get; set; }
    }
    public class Games
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int GameId { get; set; }
        public int TeamA { get; set; }
        public int TeamB { get; set; }
        public int ResultA { get; set; }
        public int ResultB { get; set; }
        public int League { get; set; }
        public DateTime StartDate { get; set; }
        public Boolean Finished { get; set; }
    }

    public class Teams
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int TeamId { get; set; }
        public string TeamName { get; set; }
    }
}

My target is, to create Games with Results etc. The TeamA & TeamB column shoukd be the TeamID from the Teams Table.

Whats steps i have to do to set the correct relationship.

Additional Informations:

Games Table ->

GamesID = 1
TeamA = 1
TeamB = 2
ResultA = 10
ResultB = 8

Teams Table ->

TeamId = 1 
TeamName = "Manchester"

TeamId = 2
TeamName = "Zurich"

On the view ->

Manchester 10 : 8 Zurich
Was it helpful?

Solution 2

Ok, i changed the code into this:

public class GamesContext : DbContext
{
    public GamesContext()
        : base("KorbballDBContext")
    {
    }

    public DbSet<Games> Games { get; set; }
    public DbSet<Teams> Teams { get; set; }
}
public class Games
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int GameId { get; set; }
    public virtual Teams TeamA { get; set; }
    public virtual Teams TeamB { get; set; }
    public int ResultA { get; set; }
    public int ResultB { get; set; }
    public int League { get; set; }
    public DateTime StartDate { get; set; }
    public Boolean Finished { get; set; }
}

public class Teams
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int TeamId { get; set; }
    public string TeamName { get; set; }
}

My Controller looks like this:

public class GamesController : Controller
{
    GamesContext db = new GamesContext();

    //
    // GET: /Games/

    public ActionResult Index()
    {
        var games = from game in db.Games
                    join teama in db.Teams on game.TeamA.TeamId equals teama.TeamId
                    join teamb in db.Teams on game.TeamB.TeamId equals teamb.TeamId
                    select game;

        return View(games.ToList());
    }
}

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.TeamA.TeamName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ResultA)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ResultB)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.TeamB.TeamName)
    </td>
</tr>

}

OTHER TIPS

I'm not sure if this is what you want to do, but if you create it like this, with the public virtual Team, the entity framework will automatically take care of the mapping for you and you can just access these "navigation properties" like normal objects.

namespace Korbball.Models
{
    public class GamesContext : DbContext
    {
        public GamesContext()
            : base("KorbballDBContext")
        {
        }

        public DbSet<Games> Games { get; set; }
        public DbSet<Teams> Teams { get; set; }
    }
    public class Games
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int GameId { get; set; }
        public virtual Team TeamA { get; set; }
        public virtual Team TeamB { get; set; }
        public int ResultA { get; set; }
        public int ResultB { get; set; }
        public int League { get; set; }
        public DateTime StartDate { get; set; }
        public Boolean Finished { get; set; }
    }

    public class Teams
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int TeamId { get; set; }
        public string TeamName { get; set; }
    }
}

EDIT:

Ok, this is how I usually do this: I create virtual fields for all navigation properties, which in your case would only be the Teams as I already pointed out.

Then, when you create the Teams (which have to be there before the game can exist), you just add them like:

var teamM = new Team(){TeamName = "Manchester"};
var teamZ = new Team(){TeamName = "Zurich"};

// 'db' is your DbContext

db.Teams.Add(teamM);
db.Teams.Add(teamZ);
// This could also happen through some user form or something.

db.SaveChanges();  
// this is important, because only after you've safed entities to you db,
// the [DatabaseGenerated] key will be set.

var game1 = new Game();
game1.ResultA = 10;
game1.ResultB = 8;

//etc.

game1.TeamA = teamM;   
game1.TeamB = teamZ;
// we still have those from up when we created an db.SaveChanges'd them

// now save everything and the Entity Framework will take care of all the relationships

db.SaveChanges();

If you have already saved the Teams and they exist in you database, you can do something like:

var team1 = db.Teams.Find(0);  // 0 beeing the [Key]  => Manchester
// or
var team2 = db.Teams.Where(m => m.Name.Contains("Zu");  //  => Zurich

// Now do the same thing with game1:

game1.TeamA = team1;
game1.TeamB = team2;

db.SaveChanges();

I hope this is what you want to know.

If this won't work, you need to take a look at some up-to-date tutorials about entity framework code first. Lots of things can go wrong with all the initialization / database creation.

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