我想知道通過使用引用進行建模的最佳做法是什麼情況下。我正在使用MongoRepository庫。MongoDB最佳做法,供參考
public class User : Entity
{
publis string Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
public class Post : Entity
{
public string Id { get; set; }
public string Title { get; set; }
public string Summary { get; set; }
public DateTime Added { get; set; }
public User Owner { get; set; }
}
當存儲後我想僅所有者(用戶)對象,而不是整個對象底層參考。
目前我正在做這樣的,不知道的更好的方法......
var post = new Post
{
Title = "Example title",
Summary = "asd asd",
Added = DateTime.Now,
Owner = new User { Id = "someExistingUserId" }
};
postRepository.Update(post); //Save
..
//Then to get the post
var post = postRepository.GetById("previouslySavedPostId");
post.Owner = userRepository.GetById(post.Owner.Id);
return post;
userRepository和postRepository是MongoRepository類型。
這是正確的做法,以使用MongoDB的用C#解決我的問題/ MVC(4)?
DBRef和FollowReference不存在 – iYonatan 2017-01-14 14:10:29