0

在我完成我的应用程序的 SimpleMembership 步骤后,还有另一个问题......

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; }
    }
}

我的目标是创建带有结果的游戏等。TeamA 和 TeamB 列应该是 Teams 表中的 TeamID。

我必须采取哪些步骤来设置正确的关系。

附加信息:

游戏桌 ->

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

团队表->

TeamId = 1 
TeamName = "Manchester"

TeamId = 2
TeamName = "Zurich"

在视图上->

Manchester 10 : 8 Zurich
4

2 回答 2

1

我不确定这是否是您想要做的,但是如果您像这样创建它public virtual Team,实体框架将自动为您处理映射,您可以像普通对象一样访问这些“导航属性” .

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; }
    }
}

编辑

好的,这就是我通常这样做的方式:我为所有导航属性创建虚拟字段,在您的情况下,这只是我已经指出的团队。

然后,当您创建团队时(必须在游戏存在之前存在),您只需像这样添加它们:

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();

如果您已经保存了团队并且它们存在于您的数据库中,您可以执行以下操作:

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();

我希望这是你想知道的。

如果这不起作用,您需要先查看一些有关实体框架代码的最新教程。所有初始化/数据库创建都可能出错。

于 2013-09-06T14:36:10.540 回答
-1

好的,我将代码更改为:

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; }
}

我的控制器看起来像这样:

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>

}

于 2013-09-10T09:33:52.160 回答