Question

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.

Was it helpful?

Solution

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...

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top