views:

229

answers:

3

Hi,

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?

A: 

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

Pierreten
Any ideas to best practice for how to implement it myself? - I guess changing the Add/Update to setting the ID before saving?
Cederstrom
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-pefor a sample of this bad advice. I've been having some success with Fluent NHibernate, using Automapping.
Tom Bushell
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
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
A: 

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.

Tom Bushell
A: 

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