1

我做了一个像这样的课程:

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 保存到我的数据库中,所以显然我遗漏了一些东西。还需要做什么来保存一对多关系?

4

3 回答 3

0

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

于 2009-12-19T00:55:16.327 回答
0

这是一个有用的链接,显示如何在 SimpleRepository 中自己管理外键 -

subsonic-3-simplerepository

自己没有尝试过,但看起来它确实有效。

Fluent Nhibernate 会自动为您执行此外键管理,但它要复杂得多。

PS如果这有帮助,请投票。

于 2009-12-19T01:14:02.250 回答
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);
于 2009-12-20T16:51:51.053 回答