views:

240

answers:

2

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.

+4  A: 

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

Rob Conery
Excellent Rob! I was wondering if you would take contributions to the project. I was looking for Subsonic to replace Linq's version of CreateDatabase(), that does NOT have a migration path. There are other columns to, such as Enums (int), Xml, and such. Things that Linq richly supports. I'll look into getting Subsonic closer to these. :) Thanks!
eduncan911
But of course - it's how we grow :). Watch this:http://www.subsonicproject.com/docs/Submit_PatchI could use all the help you can give...
Rob Conery
A: 

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

Rob
Technically, you should have commented - not posted an answer. But being a new recruit, you don't have access to comment. :) I think that comes after u get like 10 points. Anyhoot, yeah I fixed it but didn't check in the patch for Rob C. to look at. What I found is that the SimpleRepository was still using the old SQL Scripts from the old Subsonic - not the newer Sql Query Builder they are now using. Wanted to comment to Rob C. about when that switch to use the newer stuff would happen - before checking in a patch on the older stuff.
eduncan911
Thanks for the update. I wondered why I couldn't comment...I just wanted to put my name out there as a tester if needed.
Rob