質問

私が作ったようなクラスます:

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を保存していないので、明らかに私は何かが欠けています。 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);

他のヒント

あなたは何が不足していません。 Simplerepositoryは、箱から出して、多くのものをサポートしていません。

SimpleRepositoryで外部キーを自分でmangageする方法を示します。

HERESに有用なリンク -

亜音速-3- simplerepository

それを自分自身を試してみましたが、それは実際に働くだろうように見えていないことがあります。

流暢NHibernateのは、自動的にこのforiegnキー管理を行いますが、それは多くの複雑なのです。

PSこれは参考になりました場合は、それを投票してください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top