简体   繁体   English

SubSonic简单存储库一对多

[英]SubSonic Simple Repository One-To-Many

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. 它不会将VideoCategory保存到我的数据库中,因此显然我缺少了一些东西。 What else is needed to done to save a one-to-many relationship? 保存一对多关系还需要做什么?

You're not missing anything. 您什么都不会错过。 Simplerepository doesn't support one to many out of the box. Simplerepository不支持开箱即用。

Heres a useful link that shows how to mangage foreign keys yourself in SimpleRepository - 这是一个有用的链接,显示了如何在SimpleRepository中自己管理外键-

subsonic-3-simplerepository 亚音速3简易存储库

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. Fluent Nhibernate会自动为您执行此外来密钥管理,但这要复杂得多。

PS If this was helpful, please vote it up. 附注:如果这有帮助,请投票。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM