아음속 저장소 간단한 일
-
20-09-2019 - |
문제
나는 다음과 같이 클래스:
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 으로 데이터베이스,이렇게 분명히 나는 뭔가를 누락.다른 무엇이 필요하기를 하나의 관계에 있는?
해결책
당신은 아마 그냥 다음과 같은,당신은 아마을 정돈하고 싶지만 그것도록 외국인 키 값을 가져옵 채:
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 지원하지 않는 한 많은합니다.
Heres 유용한 링크 방법을 보여주는램 관리를 외 키에서 자신 SimpleRepository-
을 시도하지 않은,그것을 자신처럼 보이지만 실제로 작동합니다.
Fluent Nhibernate 할 것입니다 이 외부 키 관리를 위한 당신이 자동으로,그러나 그것은 훨씬 더 복잡합니다.
PS 의 경우 이 도움이되었,투표하시기 바랍니다.
제휴하지 않습니다 StackOverflow