使用 ADO.NET 实体获取标识行值

发布于 2024-11-04 18:24:13 字数 736 浏览 0 评论 0原文

我在 SQL Server 2008 数据库中有下表:

User
--------------------------------------------------------
Id           Numeric(18, 0) | Identity(1, 1) PK not null
Name         Nchar(20)      | Not null

我正在使用 ADO.NET 实体数据模型来执行以下操作:

MyEntities entities;
try
{
    entities = new MyEntities();
    if (entities.User.Count(u => u.Name == userName) == 0)
    {
        entities.User.AddObject(new User()
        {
            Name = userName
        });
        resultCode = 1;
        entities.SaveChanges();
    }
    else
    {
        resultCode = 2;
    }
}
catch
{
    resultCode = 3;
}
finally
{
    if (entities != null)
        entities.Dispose();
}

如何获取添加的新用户的 User.Id?

I have the following table in SQL Server 2008 database:

User
--------------------------------------------------------
Id           Numeric(18, 0) | Identity(1, 1) PK not null
Name         Nchar(20)      | Not null

I'm using an ADO.NET Entity Data model to do:

MyEntities entities;
try
{
    entities = new MyEntities();
    if (entities.User.Count(u => u.Name == userName) == 0)
    {
        entities.User.AddObject(new User()
        {
            Name = userName
        });
        resultCode = 1;
        entities.SaveChanges();
    }
    else
    {
        resultCode = 2;
    }
}
catch
{
    resultCode = 3;
}
finally
{
    if (entities != null)
        entities.Dispose();
}

How can I get User.Id for new User added?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-11-11 18:24:13

如果您的模型配置正确并且您的 Id 属性标有 StoreGeneratePattern.Identity,您只需调用以下命令:

    var user = new User()
    {
        Name = userName
    };
    entities.User.AddObject(user);
    resultCode = 1;
    entities.SaveChanges();
    int id = user.Id; // it's here

If your model is configured correctly and you have Id property marked with StoreGeneratedPattern.Identity you need simply call this:

    var user = new User()
    {
        Name = userName
    };
    entities.User.AddObject(user);
    resultCode = 1;
    entities.SaveChanges();
    int id = user.Id; // it's here
避讳 2024-11-11 18:24:13

尝试保留对用户对象的引用。一旦调用 SaveChanges(),Id 就会自动更新。这是对您的代码的修改以进行演示:

if (entities.User.Count(u => u.Name == userName) == 0)
{
    User newUser = new User()
    {
        Name = userName
    };
    entities.User.AddObject(newUser);
    resultCode = 1;
    entities.SaveChanges();

    // newUser.Id should be populated at this point.
}

Try keeping a reference to the user object. Once SaveChanges() is called, the Id should automatically be updated. Here's a modification of your code to demonstrate:

if (entities.User.Count(u => u.Name == userName) == 0)
{
    User newUser = new User()
    {
        Name = userName
    };
    entities.User.AddObject(newUser);
    resultCode = 1;
    entities.SaveChanges();

    // newUser.Id should be populated at this point.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文