在实体框架中获取最后添加的记录ID [重复]

时间:2022-02-16 10:18:44

This question already has an answer here:

这个问题在这里已有答案:

I'm registering a new client in my database, and when that operation finishes I want to show a detail view for that new client. The problem is I'm not finding the way to send to the Details action method the ID of the recently added client.

我正在我的数据库中注册一个新客户端,当该操作完成时,我想显示该新客户端的详细视图。问题是我找不到向Details操作方法发送最近添加的客户端的ID的方法。

I'm working on this code:

我正在研究这段代码:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(ClientModel credential)
{
    if(ModelState.IsValid)
    {
        database.Clients.Add(credential);
        database.SaveChanges();

        return RedirectToAction("Details", //Here is my problem, because sending credential.ID gives me an error as far as credential doesn't have an ID due it's not a database record.;
    }
    else
    {
        return View();
    }
}

This is mi ClientModel:

这是mi ClientModel:

[Table("Clients")]
public class ClientModel
{
    [Key]
    public Int16 ID { get; set; }

    [Required]
    public String Name { get; set; }

    public String Address { get; set; }

    [EmailAddress]
    public String EmailAddress { get; set; }

    [Phone]
    public String Phone { get; set; }

    public virtual ICollection<UserModel> Users { get; set; }
}

Could someone bring light to my path?

有人能为我的道路带来光明吗?

EDIT:

编辑:

Got an answer, which I would like to discuss:

得到了答案,我想讨论一下:

return RedirectToAction("Details", new { ID = credential.ID });

This solution worked, but now the question could be, is this the better solution, or should I use 2 classes as commented in @Basic's answer?

这个解决方案有效,但现在的问题可能是,这是更好的解决方案,还是我应该在@ Basic的答案中使用2个类?

2 个解决方案

#1


8  

When you call SaveChanges(), credential.ID, if an auto number in the database and setup in the EF model, should be populated. You could try reloading an element like mentioned in Entity Framework 4.1 DbSet Reload.

当您调用SaveChanges(),credential.ID时,如果应填充数据库中的自动编号并在EF模型中设置。您可以尝试重新加载Entity Framework 4.1 DbSet Reload中提到的元素。

In your scenario above, you are adding a new client, so you need to convert the client model to client like:

在上面的场景中,您要添加一个新客户端,因此您需要将客户端模型转换为客户端,如:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(ClientModel credential)
{
    var client = new Client
    {
       Name = model.Name,
       EmailAddress = model.EmailAddress,
       .
       .
    };

    db.Clients.Add(client);
    db.SaveChanges();

    model.ID = client.ID;

    return View(model);
}

And then the ID will be returned with the model.

然后ID将与模型一起返回。

#2


4  

Once you call database.SaveChanges();, the ID field on the model will be populated with the correct Id. So the simple answer to your question is...

一旦调用database.SaveChanges();,模型上的ID字段将填充正确的Id。所以对你的问题的简单回答是......

Int16 NewId = credential.ID;

As an aside, unless you're absolutely certain you'll never need to cope with more than 32,767 of any type of record, you're usually better off to use Int for all Id fields (or even Long) - that way you don't need to remember which are Int16 and which are Int. The few bytes you save by halving the length of some Id fields are absolutely negligible unless you're on an embedded system.

顺便说一下,除非你绝对肯定你永远不需要处理超过32,767的任何类型的记录,你通常最好在所有Id字段(甚至是Long)中使用Int - 这样你就不会我们需要记住哪些是Int16,哪些是Int。除非你在嵌入式系统上,否则通过将某些Id字段的长度减半而节省的几个字节绝对可以忽略不计。

#1


8  

When you call SaveChanges(), credential.ID, if an auto number in the database and setup in the EF model, should be populated. You could try reloading an element like mentioned in Entity Framework 4.1 DbSet Reload.

当您调用SaveChanges(),credential.ID时,如果应填充数据库中的自动编号并在EF模型中设置。您可以尝试重新加载Entity Framework 4.1 DbSet Reload中提到的元素。

In your scenario above, you are adding a new client, so you need to convert the client model to client like:

在上面的场景中,您要添加一个新客户端,因此您需要将客户端模型转换为客户端,如:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(ClientModel credential)
{
    var client = new Client
    {
       Name = model.Name,
       EmailAddress = model.EmailAddress,
       .
       .
    };

    db.Clients.Add(client);
    db.SaveChanges();

    model.ID = client.ID;

    return View(model);
}

And then the ID will be returned with the model.

然后ID将与模型一起返回。

#2


4  

Once you call database.SaveChanges();, the ID field on the model will be populated with the correct Id. So the simple answer to your question is...

一旦调用database.SaveChanges();,模型上的ID字段将填充正确的Id。所以对你的问题的简单回答是......

Int16 NewId = credential.ID;

As an aside, unless you're absolutely certain you'll never need to cope with more than 32,767 of any type of record, you're usually better off to use Int for all Id fields (or even Long) - that way you don't need to remember which are Int16 and which are Int. The few bytes you save by halving the length of some Id fields are absolutely negligible unless you're on an embedded system.

顺便说一下,除非你绝对肯定你永远不需要处理超过32,767的任何类型的记录,你通常最好在所有Id字段(甚至是Long)中使用Int - 这样你就不会我们需要记住哪些是Int16,哪些是Int。除非你在嵌入式系统上,否则通过将某些Id字段的长度减半而节省的几个字节绝对可以忽略不计。