SubSonic简单存储库一对多

时间:2009-12-19 00:53:25

标签: subsonic subsonic3 one-to-many simplerepository

我做了一个类:

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);
    }
}

另一个喜欢

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);
    }
}

然后我有类似的代码:

        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();

它没有将VideoCategory保存到我的数据库中,所以很明显我错过了一些东西。还需要做些什么才能保存一对多关系?

3 个答案:

答案 0 :(得分:0)

你没有遗漏任何东西。 Simplerepository不支持开箱即用的一对多。

答案 1 :(得分:0)

这是一个有用的链接,展示了如何在SimpleRepository中自行解决外键 -

subsonic-3-simplerepository

我自己没试过,但看起来确实会有用。

Fluent Nhibernate将自动为您进行密钥管理,但它更复杂。

PS如果这有用,请将其投票。

答案 2 :(得分:0)

您可能只是执行以下操作,您可能想要整理它,但它会确保您的外键值被填充:

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);