如何设置模型以执行此事务?

时间:2015-10-03 05:58:26

标签: c# sql asp.net asp.net-mvc entity

我的部分数据库是用

创建的
/*
   Create table of scores of games played. Every game will have a score recorded, but there
   will only be a corresponding name if the user enters one
*/
CREATE TABLE Scores ( id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
                      score int NOT NULL,
                      name VARCHAR (50) 
                    );

/* 
    Create table of text logs of the games played. These are reviewed to sniff out cheating.  
*/
CREATE TABLE GameLogs ( id int IDENTITY(1,1) NOT NULL PRIMARY KEY, 
                        scoreId INT NOT NULL FOREIGN KEY REFERENCES scores(id) ON DELETE CASCADE ON UPDATE CASCADE, 
                        logText VARCHAR (8000)
                       ); 

我正在使用Entity框架生成的模型来尝试执行以下事务的等效操作。

 INSERT INTO Scores (score, name) VALUES (someNumber, someString); 
 SELECT MAX(id) as new_id FROM Scores; 
 INSERT INTO GameLogs (scoreId, logText) VALUES (new_id, someOtherString);

实体生成的相应类是

public partial class Score
{
    public Score()
    {
        GameLogs = new HashSet<GameLog>();
    }

    public int id { get; set; }

    [Column("score")]
    public int score1 { get; set; }

    [StringLength(50)]
    public string name { get; set; }

    public virtual ICollection<GameLog> GameLogs { get; set; }
}
public partial class GameLog
{
    public int id { get; set; }

    public int scoreId { get; set; }

    [StringLength(8000)]
    public string logText { get; set; }

    public virtual Score Score { get; set; }
}
public partial class SnakeDb : DbContext
{
    public SnakeDb()
        // Comment/uncomment the base(...) depending on where project is being deployed at the moment
        // Local Database:
        //: base("name=snakedb")
        // Remove database: 
        : base("name=remote_snakedb")
    {
    }

    public virtual DbSet<BannedIP> BannedIPs { get; set; }
    public virtual DbSet<GameLog> GameLogs { get; set; }
    public virtual DbSet<IP> IPs { get; set; }
    public virtual DbSet<Score> Scores { get; set; }

    protected override void OnModelCreating ( DbModelBuilder modelBuilder )
    {
        modelBuilder.Entity<GameLog>()
            .Property(e => e.logText)
            .IsUnicode(false);

        modelBuilder.Entity<IP>()
            .HasMany(e => e.BannedIPs)
            .WithRequired(e => e.IP)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Score>()
            .Property(e => e.name)
            .IsUnicode(false);
    }
}

在我的一个控制器中,我有方法

    [HttpPost]
    public ActionResult SubmitScore ( NameScoreLog nsp )
    {

        // Submit name and score to Scores database
        SD.Scores.Add( new Score { name = nsp.name, score1 = nsp.score } );

        // ... 

        SD.SubmitChanges();

    }

这是我计划执行我提到的交易的地方。现在,我完全不知道如何从添加到id的最后一个元素中提取SD.Scores,即相当于

SELECT MAX(id) as new_id FROM Scores; 
顺便说一句,我认识到这是获得id最后一次加分的一种有缺陷的方法,因为在我添加得分和进行上述查询之间可以插入另一个得分。

有人可以就如何解决这个问题给我一些指导吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

  var score = new Score { name = nsp.name, score1 = nsp.score };
  SD.Scores.ADD(score);
  db.savechanges();
  var id = score .Id;