我试图强制实体框架5.0在我生成的每个poco类上创建一个构造函数。此构造函数应该实例化我拥有的任何外键导航属性。
e.g。
public partial class Event
{
public System.Guid EventId { get; set; }
public System.DateTime CreatedDate { get; set; }
public string CreatedUser { get; set; }
public int CreatedUserId { get; set; }
public string Title { get; set; }
public string EventDesc { get; set; }
public System.DateTime Start { get; set; }
public System.DateTime End { get; set; }
public string Source { get; set; }
public bool Editable { get; set; }
public string ClassName { get; set; }
public string Url { get; set; }
public bool IsDeleted { get; set; }
public bool IsObsolete { get; set; }
public bool AllDay { get; set; }
public System.DateTime ModifiedDate { get; set; }
public string ModifiedUser { get; set; }
public int RowVer { get; set; }
public virtual UserProfile UserProfile { get; set; }
}
应该成为:
public partial class Event
{
public Event()
{
this.UserProfile = new UserProfile();
}
public System.Guid EventId { get; set; }
public System.DateTime CreatedDate { get; set; }
public string CreatedUser { get; set; }
public int CreatedUserId { get; set; }
public string Title { get; set; }
public string EventDesc { get; set; }
public System.DateTime Start { get; set; }
public System.DateTime End { get; set; }
public string Source { get; set; }
public bool Editable { get; set; }
public string ClassName { get; set; }
public string Url { get; set; }
public bool IsDeleted { get; set; }
public bool IsObsolete { get; set; }
public bool AllDay { get; set; }
public System.DateTime ModifiedDate { get; set; }
public string ModifiedUser { get; set; }
public int RowVer { get; set; }
public virtual UserProfile UserProfile { get; set; }
}
我知道这是可能的,但不知道如何。非常感激任何的帮助。 感谢
当我从我的存储库中的db中检索时(见下文),我创建了一个事件列表,当我通过json传回这个事件列表时,由于event.UserProfile为null,我得到一个解析错误。 我可以在每个事件的代码中设置它,但这不会很聪明。 如果可能的话,我需要一个链接或一个例子来帮助实现我的需要。
public List<Event> GetEvents(int userId, DateTime start, DateTime end)
{
List<Event> domainList = new List<Event>();
using (BookingModels dbEntities = new BookingModels())
{
var eventQuery = from dboEvents in dbEntities.Events
where dboEvents.Start >= start
&& dboEvents.End <= end
select dboEvents;
domainList = eventQuery.ToList<Event>();
}
return domainList;
}