如何获取与 SubSonic 3 LinqTemplate 插入关联的标识列值?

发布于 2024-08-07 18:19:36 字数 1017 浏览 3 评论 0原文

我正在使用 SubSonic 3.0.0.3 以及 Linq T4 模板。例如,我的 ProjectRepository 有以下两种方法:

public int Add(Project item)
{
    int result = 0;
    ISqlQuery query = BuildInsertQuery(item);

    if (query != null)
    {
       result = query.Execute();
    }
    return result;
}

private ISqlQuery BuildInsertQuery(Project item)
{
    ITable tbl = FindTableByClassName();

    Insert query = null;
    if (tbl != null)
    {
        Dictionary<string, object> hashed = item.ToDictionary();
        query = new Insert(_db.Provider).Into<Project>(tbl);

        foreach (string key in hashed.Keys)
        {
            IColumn col = tbl.GetColumn(key);
            if (col != null)
            {
                if (!col.AutoIncrement)
                {
                    query.Value(key, hashed[key]);
                }
            }
        }
    }

    return query;
}

除了执行插入(效果很好)之外,我真的很想获取自动递增 ProjectId 列的值。根据记录,该列既是主键又是标识列。也许有办法附加“SELECT SCOPE_IDENTITY();”到查询或者也许我应该尝试一种完全不同的方法?

I am using SubSonic 3.0.0.3 along with the Linq T4 Templates. My ProjectRepository, for example, has the following two methods:

public int Add(Project item)
{
    int result = 0;
    ISqlQuery query = BuildInsertQuery(item);

    if (query != null)
    {
       result = query.Execute();
    }
    return result;
}

private ISqlQuery BuildInsertQuery(Project item)
{
    ITable tbl = FindTableByClassName();

    Insert query = null;
    if (tbl != null)
    {
        Dictionary<string, object> hashed = item.ToDictionary();
        query = new Insert(_db.Provider).Into<Project>(tbl);

        foreach (string key in hashed.Keys)
        {
            IColumn col = tbl.GetColumn(key);
            if (col != null)
            {
                if (!col.AutoIncrement)
                {
                    query.Value(key, hashed[key]);
                }
            }
        }
    }

    return query;
}

Along with performing the insert (which works great), I'd really like to get the value of the auto-incrementing ProjectId column. For the record, this column is both the primary key and identity column. Is there perhaps a way to append "SELECT SCOPE_IDENTITY();" to the query or maybe there's an entirely different approach which I should try?

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

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

发布评论

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

评论(1

一张白纸 2024-08-14 18:19:36

您可以使用 ActiveRecord 模板来完成此操作,该模板会为您完成上述所有连接(并且还具有内置测试)。在您的场景中,Add 方法将只有一行:Project.Add(),它将返回新的 id。

根据您的需要,您可以尝试以下操作:

var cmd=query.GetCommand();
cmd.CommandSql+=";SELECT SCOPE_IDENTITY() as newid";
var newID=query.Provider.ExecuteScalar(cmd);

应该可以工作..

*编辑 - 您也可以在 ISqlQuery 上为此创建一个 ExtensionMethod,以节省一些写作...

You can do this with the ActiveRecord templates which does all of the wiring above for you (and also has built-in testing). In your scenario, the Add method would have one line: Project.Add() and it would return the new id.

For your needs, you can try this:

var cmd=query.GetCommand();
cmd.CommandSql+=";SELECT SCOPE_IDENTITY() as newid";
var newID=query.Provider.ExecuteScalar(cmd);

That should work..

*Edit - you can create an ExtensionMethod for this on ISqlQuery too, to save some writing...

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