我如何设置我的模型来执行此交易?
How can I set up my model to perform this transaction?
我有一部分数据库是用
创建的
/*
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 framework 生成的模型来尝试执行以下交易的等效项。
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);
Entity生成对应的类是
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();
}
这是我计划执行我提到的交易的地方。现在我完全不知道我将如何从添加到 SD.Scores
的最后一个元素中提取 id
,即做相当于
SELECT MAX(id) as new_id FROM Scores;
顺便说一句,我意识到这是获得 id
最后添加分数的一种有缺陷的方法,因为在我添加分数和进行上述操作之间可以插入另一个分数查询。
有人可以指导我如何解决这个问题吗?
试试这个:
var score = new Score { name = nsp.name, score1 = nsp.score };
SD.Scores.ADD(score);
db.savechanges();
var id = score .Id;
我有一部分数据库是用
创建的/*
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 framework 生成的模型来尝试执行以下交易的等效项。
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);
Entity生成对应的类是
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();
}
这是我计划执行我提到的交易的地方。现在我完全不知道我将如何从添加到 SD.Scores
的最后一个元素中提取 id
,即做相当于
SELECT MAX(id) as new_id FROM Scores;
顺便说一句,我意识到这是获得 id
最后添加分数的一种有缺陷的方法,因为在我添加分数和进行上述操作之间可以插入另一个分数查询。
有人可以指导我如何解决这个问题吗?
试试这个:
var score = new Score { name = nsp.name, score1 = nsp.score };
SD.Scores.ADD(score);
db.savechanges();
var id = score .Id;