【问题标题】:SubSonic Simple Repository One-To-ManySubSonic 简单存储库一对多
【发布时间】:2009-12-19 00:53:24
【问题描述】:

我做了一个这样的类:

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

【问题讨论】:

    标签: subsonic subsonic3 one-to-many simplerepository


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 关于如何自己实现它的最佳实践有什么想法吗? - 我猜想在保存之前将添加/更新更改为设置 ID?
      • 这是真的,但在 SO 上有几个非常具有误导性的“答案”暗示并非如此。请参阅我的问题stackoverflow.com/questions/1610362/… 以获取此错误建议的示例。我使用 Automapping 在 Fluent NHibernate 方面取得了一些成功。
      • Tom:我非常喜欢 subsonic 并且它的易用性,并且渴望在新项目中使用 simplerepository;不幸的是,我遇到了这个问题,这使我成为了交易破坏者。迫不及待地想看到从我的代码中推断出的关系模式!
      • 是的,亚音速为了简单性和可用性而发挥作用。我相信它最终会支持开箱即用的一对多,但目前还没有,唉。
      【解决方案3】:

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

      subsonic-3-simplerepository

      我自己没试过,但看起来确实可以。

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

      PS 如果有帮助,请点赞。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多