0
事件
public class Event
{
public Guid? UserID { get; set; }
[ForeignKey("UserID")]
public virtual User User { get; set; }
...
用戶
public class User
{
public Guid UserId { get; set; }
// Not used in this example, but just thought they might be related to problem
private List<Event> _attendedEvents;
public virtual ICollection<Event> AttendedEvents
{
get { return _attendedEvents ?? (_attendedEvents = new List<Event>()); }
set {
if (value == null)
_attendedEvents = new List<Event>();
else
_attendedEvents = new List<Event>(value);
}
}
public virtual ICollection<Event> HostedEvents { get; set; }
...
EventConfiguration
HasOptional<User>(s => s.User)
.WithMany(s => s.HostedEvents)
.HasForeignKey(s => s.UserID);
- 我想要做的事情是
- 添加用戶到資源庫
- 添加事件(這在其內部具有相同的用戶)到資源庫
- 保存更改
- 從庫中獲取事件回
所有類型的作品,除了當我檢索事件回它有空用戶,但UserId是有效的,並指向我以前創建的用戶。
以下是我正在做
// Creates just the User object with specified UserName
var user = ObjectHelpers.CreateUser("ServiceTestUser");
// Adds to Repository + Saves Changes
_client.AddUser(user);
// Find it again to have generated Id and kind of test if it was added
user = _client.FindUserByEmail(user.Email);
// Create Event object and assign specified user object to it
// At this point @event has User set to above one and UserID null
var @event = ObjectHelpers.CreateEvent(user);
// Attach User from Event + Add Event to repository + Save Changes
_client.AddEvent(@event);
// Get it back to Check if everything went fine
// At this point @event has User set to null and valid UserID
@event = _client.GetEventByTitle(@event.EventTitle);
GetEventByTitle實際上在做什麼? –
返回第一個或默認的事件匹配標題屬性,我沒有提到 –