1

I made a class like:

public class Video
{
    public Guid VideoID { get; set; }
    public VideoCategory VideoCategory { get; set; }
    public int SortIndex { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
    public string Author { get; set; }
    public string Filename { get; set; }

    public new void Add()
    {
        this.VideoID = Guid.NewGuid();
        DB.Repository.Add(this);
    }
}

And another like

public class VideoCategory
{
    public Guid VideoCategoryID { get; set; }
    public string Title { get; set; }

    public new void Add()
    {
        this.VideoCategoryID = Guid.NewGuid();
        DB.Repository.Add(this);
    }
}

I then have code like:

        VideoCategory VideoCategory = new VideoCategory();
        VideoCategory.Title = "TestTitle";
        VideoCategory.Add();

        Video Video = new Video();
        Video.VideoCategory = VideoCategory;
        Video.SortIndex = 1;
        Video.Title = "TestTitle";
        Video.Body = "TestBody";
        Video.Author = "TestAuthor";
        Video.Filename = "TestFile.flv";
        Video.Add();

It doesn't save the VideoCategory into my database, so obviously i'm missing something. What else is needed to done to save a one-to-many relationship?

Cederstrom
  • 58
  • 4

3 Answers3

0

You're not missing anything. Simplerepository doesn't support one to many out of the box.

Pierreten
  • 9,917
  • 6
  • 37
  • 45
  • Any ideas to best practice for how to implement it myself? - I guess changing the Add/Update to setting the ID before saving? – Cederstrom Dec 19 '09 at 01:04
  • This is true, but there are several very misleading "answers" on SO that imply otherwise. See my question http://stackoverflow.com/questions/1610362/subsonic-can-anyone-provide-an-example-of-using-subsonic-simplerepository-to-pe for a sample of this bad advice. I've been having some success with Fluent NHibernate, using Automapping. – Tom Bushell Dec 19 '09 at 01:08
  • Tom: I absolutely love subsonic and it's ease of use, and was dying to use the simplerepository on a new project; I unfortunately ran into this issue which made this I dealbreaker. Can't wait to see relational schemas getting inferred from my code! – Pierreten Dec 19 '09 at 01:13
  • Yeah, subsonic kicks ass for simplicity and usability. I believe it will eventually support one-to-many out of the box, but not yet, alas. – Tom Bushell Dec 19 '09 at 01:20
0

Heres a useful link that shows how to mangage foreign keys yourself in SimpleRepository -

subsonic-3-simplerepository

Have not tried it myself, but looks like it would actually work.

Fluent Nhibernate will do this foriegn key management for you automatically, but it's a LOT more complex.

PS If this was helpful, please vote it up.

Community
  • 1
  • 1
Tom Bushell
  • 5,865
  • 4
  • 45
  • 60
0

You could probably just do the following, you'll probably want to tidy it up but it will ensure your foreign key value gets populated:

public class Video
{
    protected VideoCategory videoCategory;
    public Guid ID { get; set; }
    public VideoCategory VideoCategory 
    {
        get { return videoCategory; }
        set
        {
            videoCategory = value;
            VideoCategoryId = value.ID;
        }
    }
    public Guid VideoCategoryId { get; set; }
    public int SortIndex { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
    public string Author { get; set; }
    public string Filename { get; set; }
}

public class VideoCategory
{
    public Guid ID { get; set; }
    public string Title { get; set; }
}

SimpleRepository repo = new SimpleRepository(SimpleRepositoryOptions.RunMigrations);

VideoCategory videoCategory = new VideoCategory();
videoCategory.ID = Guid.NewGuid();
videoCategory.Title = "TestTitle";
repo.Add<VideoCategory>(videoCategory);

Video video = new Video();
video.ID = Guid.NewGuid();
video.VideoCategory = videoCategory;
video.SortIndex = 1;
video.Title = "TestTitle";
video.Body = "TestBody";
video.Author = "TestAuthor";
video.Filename = "TestFile.flv";
repo.Add<Video>(video);
Adam Cooper
  • 8,077
  • 2
  • 33
  • 51