SubSonic 生成的数据库模式,二进制类型?

发布于 2024-08-04 09:53:35 字数 1909 浏览 7 评论 0原文

如何使用 SubSonic 的架构生成器在数据库中创建二进制列?

所以今晚我决定深入研究 SubSonic。我在这里看到了很多问题,以及 Rob 和其他许多人的精彩回答。我看到 SubSonic 是一个 ORM,使用 T4 模板,它可以从现有数据库生成一些非常漂亮且高效的类。

但是,我想走另一条路。我有一个非常丰富的域,并且想要使用 SubSonic 和 RunMigrations 选项从我的域创建临时表。

一切都运行得非常好(甚至编写了我自己的 Upgrade() 函数,该函数根据程序集修订号检测代码库是否发生更改,然后将所有对象更新迁移到数据库 - 对于自动升级数据库来说非常灵活和高效)。

但是,如何让 SubSonic 创建二进制列呢?也许我没有按预期使用它(我没有使用 Query 或 SqlQuery,只是使用 SimpleRepository 的 Linq 接口)。请参见下文(使用常见的“博客文章”示例):

[SubsonicTable]
public class Post
{
    [SubSonicPrimaryKey]
    public Int32 PostID { get; set; }

    [SubSonicStringLength(1024)]
    public String Title { get; set; }

    [SubSonicLongString]
    public String Body { get; set; }

    [SubSonicStringLength(5)]
    public String? LangaugeCode { get; set; }

    public DateTime? Published { get; set; }

    public PostType PostType { get; set; }
    public Int32 PostTypeID
    {
        get
        {
            return this.PostType.GetHashCode();
        }
        set
        {
            Enum.Parse(typeof(PostType), value.ToString());
        }
    }

    public Byte[] Image { get; set; }
}

public enum PostType
{
    NotSet = 0
    ,BlogPost
    ,Comment
    ,Trackback
    ,Pingback
}

当通过 SimpleRepository 保存或查询此 Post 对象时,它缺少两列:PostType(或类型枚举 PostType)和 Image(类型为 byte[] 数组) 。

现在,我在这里找到了有人发布的有关使用 Int32 PostTypeID 来解决枚举问题的 hack-of-an-answer。来吧,Rob,SubSonic 应该能够支持枚举类型到 INT,以及从它们返回。 ;) 这就是为什么我有 PostTypeID,并且它被正确创建和写入。

下面是为我创建 Post 表以及插入第一篇文章的命令示例:

Post p = new Post();
p.Title = "My Title";
p.Body = "The body of the post.";
p.PostType = PostType.BlogPost;

var repo = new SimpleRepository(SimpleRepositoryOptions.RunMigrations);
repo.Add(p);

注意:您不应该在生产中使用此代码,因为 RunMigrations 在首次运行时有许多额外的 TSQL 查询。

您可以通过上面的示例看到,如果 Posts 表不存在,这将创建它,并创建列。

但是,这不会创建上面提到的两列:PostType 或 Image。

想法?并提前致谢。

How do you create binary columns in the DB using SubSonic's Schema builder?

So tonight I decided to dive into SubSonic. I see a lot of questions on here and great responses by Rob and many others. I see that SubSonic is an ORM, with the T4 Templates it can generate some very nice and efficient classes from an existing database.

But, I want to go the other way. I have a very rich Domain, and want to create my tables adhoc from my Domain using SubSonic and the RunMigrations option.

All works extremely well (even wrote my own Upgrade() function that detects if the codebase has changes based on the Assembly revision number, and then migrates all object updates to the DB - pretty slick and efficent for auto-upgrading the DB).

But, how do you have SubSonic create binary columns? Maybe I am not using it as intended (I am not using Query or SqlQuery, just the Linq interface of SimpleRepository). See below (using a common "Blog Post" example):

[SubsonicTable]
public class Post
{
    [SubSonicPrimaryKey]
    public Int32 PostID { get; set; }

    [SubSonicStringLength(1024)]
    public String Title { get; set; }

    [SubSonicLongString]
    public String Body { get; set; }

    [SubSonicStringLength(5)]
    public String? LangaugeCode { get; set; }

    public DateTime? Published { get; set; }

    public PostType PostType { get; set; }
    public Int32 PostTypeID
    {
        get
        {
            return this.PostType.GetHashCode();
        }
        set
        {
            Enum.Parse(typeof(PostType), value.ToString());
        }
    }

    public Byte[] Image { get; set; }
}

public enum PostType
{
    NotSet = 0
    ,BlogPost
    ,Comment
    ,Trackback
    ,Pingback
}

When this Post object gets saved, or queried, via the SimpleRepository, it is missing two columns: PostType (or type enum PostType) and Image (of type byte[] array).

Now, I found the hack-of-an-answer here that someone posted about using an Int32 PostTypeID to get around the enum issue. Come on Rob, SubSonic should be able to support enum types to INT, and back from them. ;) This is why I have the PostTypeID, and this gets created and written properly.

Here's an example of the command that creates the Post table for me, as well as inserting the first post:

Post p = new Post();
p.Title = "My Title";
p.Body = "The body of the post.";
p.PostType = PostType.BlogPost;

var repo = new SimpleRepository(SimpleRepositoryOptions.RunMigrations);
repo.Add(p);

NOTE: You should not use this code in production, as the RunMigrations has lots of additional TSQL queries at first run.

You can see by the example above that this will create the Posts table if it doesn't exist, as well as creating the columns.

But, this does not create the two columns mentioned above: PostType nor Image.

Thoughts? And thanks in advance.

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

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

发布评论

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

评论(2

海夕 2024-08-11 09:53:35

以为我们有一个二进制嗅探器 - 结果它不起作用。我需要添加这个 - 如果你不介意分叉我们拥有的东西并添加这个,那就更好了 - 我会喜欢你的。抢夺我们的扩展内容 - 你想修改 ToDataTable() (我认为)...

如果你没有机会 - 我会在下一步加速 SimpleRepo 时添加这个...

I had thought we had a binary sniffer - turns out it doesn't do the trick. I need to add this - better yet if you wouldn't mind forking what we have and adding this - I'd love you for it. Take a loot at our Extensions stuff - you want to mod ToDataTable() (I think)...

If you don't get a chance - I'll add this when I rev up SimpleRepo next...

却一份温柔 2024-08-11 09:53:35

我自己刚刚遇到了二进制列问题。如果您有补丁/修复程序,我愿意测试它。

I just ran into the Binary column issue myself. If you have a patch/fix I'd be willing to test it.

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