在SubSonic 3中插入新的记录模式

时间:2023-01-29 13:51:22

I'm trying out the new SubSonic 3 preview, but I'm not sure about the patterns I should be using for the basic CRUD operations in my MVC project.

我正在尝试新的SubSonic 3预览,但我不确定我应该用于我的MVC项目中的基本CRUD操作的模式。

I'm trying to keep as much of my data logic in my models, so I added some static CRUD methods to each model's partial class.

我试图在我的模型中保留尽可能多的数据逻辑,所以我为每个模型的部分类添加了一些静态CRUD方法。

For example, let's say I have a configuration table, which should only have a single record. So, my partial class may look something like this:

例如,假设我有一个配置表,它应该只有一个记录。所以,我的部分课可能看起来像这样:

public partial class Configuration{
        // cut down; mocking hooks here IRL
        private static IRepository<Configuration> Table = new MyRepository<Configuration>();

        public static Configuration Retrieve()
        {
            var config = Table.GetAll().FirstOrDefault();
            if (config == null)
            {
                config = new Configuration();
                Table.Add(config);
            }
            return config;
        }

        public static void Update(Configuration target)
        {
            Table.Update(target);
        }
}

Currently, this doesn't work as the config table has an identity column for a primary key, and Add-ing a new record to the table throws the standard "Cannot insert explicit value for identity column" error. SubSonic 3 doesn't seem to generate classes that, upon new-ing them up, play nice with the rules of the database schema (i.e., no default values, no nullable primitives for values that are nullable in the database, etc).

目前,这不起作用,因为配置表具有主键的标识列,并且向表添加新记录会引发标准“无法为标识列插入显式值”错误。 SubSonic 3似乎没有生成类,这些类在新建它们时,可以很好地使用数据库模式的规则(即,没有默认值,对于可在数据库中为空的值,没有可空的基元等)。

Now, I can alter my table and pattern to get around these issues, but I'm wondering about when I cannot get around this issue--when I have to add a new record to the database and have an identity as my primary key.

现在,我可以改变我的表和模式以解决这些问题,但我想知道何时我无法解决这个问题 - 当我必须向数据库添加新记录并将身份作为我的主键时。

I'm also wondering if this pattern is even correct or not. SubSonic gives you a number of different ways you can go about your repository business, so I'm not sure which one I should be using. I'd LIKE to use my models as MUCH as possible (otherwise why not just Linq to Sql?), so I don't want to use SubSonic's query building goodness when trying to CRUD my models.

我也想知道这种模式是否正确。 SubSonic为您提供了许多不同的存储库业务方式,因此我不确定应该使用哪种方式。我想尽可能多地使用我的模型(否则为什么不只是Linq to Sql?),所以我不想在尝试CRUD我的模型时使用SubSonic的查询建立良好性。

What should I do here? Any advice on CRUD patterns for using SubSonic 3 in my MVC project would be welcome and +'d. Also welcome are links to websites that cover this subject for SubSonic 3 but that don't rank high in Google searches...

我该怎么办?关于在我的MVC项目中使用SubSonic 3的CRUD模式的任何建议都是受欢迎的,并且+'d。也欢迎链接到SubSonic 3涵盖此主题的网站,但在Google搜索中排名不高......


Asked Rob directly (link here). For my DB, at least, there's a showstopper bug in the generated code. Aaah, alpha software.

直接问罗布(链接在这里)。对于我的数据库,至少,生成的代码中有一个showstopper错误。 Aaah,alpha软件。


UPDATE

With the release of Subsonic3, can we have a little bump to reconsider this question?

随着Subsonic3的发布,我们可以稍微重新考虑一下这个问题吗?

1 个解决方案

#1


First and foremost: the t4 templates are there for you to change as needed with SS3. That was the main idea with using T4 - I don't want to back you into my silliness :).

首先:t4模板可供您根据需要使用SS3进行更改。这是使用T4的主要想法 - 我不想让你回到我的愚蠢:)。

To the question at hand - I think this might be a bug in our templates that refuses to stick a value into the PK field:

对于手头的问题 - 我认为这可能是我们的模板中的一个错误,它拒绝将值粘贴到PK字段中:

    ISqlQuery BuildInsertQuery(T item) {
        ITable tbl = _db.FindTable(typeof(T).Name);
        Insert query = null;
        if (tbl != null) {
            var hashed = item.ToDictionary();
            query = new Insert(_db.Provider).Into<T>(tbl); ;
            foreach (string key in hashed.Keys) {
                IColumn col = tbl.GetColumn(key);
                if (!col.IsPrimaryKey) {
                    query.Value(key, hashed[key]);
                }
            }
        }
        return query;
    }

In this, our check should actually be...

在这,我们的检查应该是......

                if (!col.IsPrimaryKey && !col.AutoIncrement) {
                    query.Value(key, hashed[key]);
                }

In this way, the non-identity will be inserted. But in reading your issue here, it sounds to me like you're not trying to insert into a non-identity.

这样,将插入非身份。但是在这里阅读你的问题时,听起来像你并没有试图插入一个非身份。

The email you sent me doesn't say anything about PKs as identity - your PK was a thing called "NAme" which is a string type and not and identity (auto-increment).

你发给我的电子邮件没有说PK作为身份 - 你的PK是一个名为“NAme”的东西,它是一个字符串类型而不是身份(自动增量)。

I'm wondering about when I cannot get around this issue--when I have to add a new record to the database and have an identity as my primary key.

我想知道什么时候我无法解决这个问题 - 当我必须向数据库添加新记录并将身份作为我的主键时。

This is what SubSonic assumes - that your PK is an IDENTITY column. If you ONLY have an IDENTITY column, we can't help you because this is a deadlocked table in that you can't insert any value into it, therefor you can't tick the IDENTITY column. Your only recourse at this point is to SET IDENTITY INSERT="off", which defeats the purpose.

这就是SubSonic所假设的 - 您的PK是IDENTITY列。如果您只有IDENTITY列,我们无法帮助您,因为这是一个死锁表,因为您无法在其中插入任何值,因此您无法勾选IDENTITY列。此时你唯一的办法就是设置IDENTITY INSERT =“off”,这会破坏目的。

Hopefully this will answer your question? If I'm not getting it - can you do this for me:

希望这会回答你的问题?如果我没有得到它 - 你能为我这样做吗:

  1. One sentence: what can't you do and what's the error
  2. 一句话:你不能做什么,错误是什么

  3. What did you expect
  4. 你期待什么

Thanks Will and I hope I'm not being thick.

谢谢威尔,我希望我不会厚。

#1


First and foremost: the t4 templates are there for you to change as needed with SS3. That was the main idea with using T4 - I don't want to back you into my silliness :).

首先:t4模板可供您根据需要使用SS3进行更改。这是使用T4的主要想法 - 我不想让你回到我的愚蠢:)。

To the question at hand - I think this might be a bug in our templates that refuses to stick a value into the PK field:

对于手头的问题 - 我认为这可能是我们的模板中的一个错误,它拒绝将值粘贴到PK字段中:

    ISqlQuery BuildInsertQuery(T item) {
        ITable tbl = _db.FindTable(typeof(T).Name);
        Insert query = null;
        if (tbl != null) {
            var hashed = item.ToDictionary();
            query = new Insert(_db.Provider).Into<T>(tbl); ;
            foreach (string key in hashed.Keys) {
                IColumn col = tbl.GetColumn(key);
                if (!col.IsPrimaryKey) {
                    query.Value(key, hashed[key]);
                }
            }
        }
        return query;
    }

In this, our check should actually be...

在这,我们的检查应该是......

                if (!col.IsPrimaryKey && !col.AutoIncrement) {
                    query.Value(key, hashed[key]);
                }

In this way, the non-identity will be inserted. But in reading your issue here, it sounds to me like you're not trying to insert into a non-identity.

这样,将插入非身份。但是在这里阅读你的问题时,听起来像你并没有试图插入一个非身份。

The email you sent me doesn't say anything about PKs as identity - your PK was a thing called "NAme" which is a string type and not and identity (auto-increment).

你发给我的电子邮件没有说PK作为身份 - 你的PK是一个名为“NAme”的东西,它是一个字符串类型而不是身份(自动增量)。

I'm wondering about when I cannot get around this issue--when I have to add a new record to the database and have an identity as my primary key.

我想知道什么时候我无法解决这个问题 - 当我必须向数据库添加新记录并将身份作为我的主键时。

This is what SubSonic assumes - that your PK is an IDENTITY column. If you ONLY have an IDENTITY column, we can't help you because this is a deadlocked table in that you can't insert any value into it, therefor you can't tick the IDENTITY column. Your only recourse at this point is to SET IDENTITY INSERT="off", which defeats the purpose.

这就是SubSonic所假设的 - 您的PK是IDENTITY列。如果您只有IDENTITY列,我们无法帮助您,因为这是一个死锁表,因为您无法在其中插入任何值,因此您无法勾选IDENTITY列。此时你唯一的办法就是设置IDENTITY INSERT =“off”,这会破坏目的。

Hopefully this will answer your question? If I'm not getting it - can you do this for me:

希望这会回答你的问题?如果我没有得到它 - 你能为我这样做吗:

  1. One sentence: what can't you do and what's the error
  2. 一句话:你不能做什么,错误是什么

  3. What did you expect
  4. 你期待什么

Thanks Will and I hope I'm not being thick.

谢谢威尔,我希望我不会厚。