C# 实体框架5从数据库创建模型,在构造函数中实例化导航属性

C# 实体框架5从数据库创建模型,在构造函数中实例化导航属性,c#,asp.net,entity-framework-5,C#,Asp.net,Entity Framework 5,我试图强制EntityFramework5.0在我生成的每个poco类上创建一个构造函数。这个构造函数应该实例化我拥有的任何外键导航属性 e、 g 应成为: public partial class Event { public Event() { this.UserProfile = new UserProfile(); } public System.Guid EventId { get; set; } public System

我试图强制EntityFramework5.0在我生成的每个poco类上创建一个构造函数。这个构造函数应该实例化我拥有的任何外键导航属性

e、 g

应成为:

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;
    }
public List GetEvents(int userId、DateTime start、DateTime end)
{
List domainList=新列表();
使用(BookingModels dbEntities=new BookingModels())
{
var eventQuery=来自dbEntities.Events中的dboEvents
其中dboEvents.Start>=Start

&&dboEvents.End您需要调整T4模板,用于实体生成。另一方面,这很有趣,为什么您需要它?我不明白您为什么要这样做?使属性虚拟化不够,如果它为空,至少您知道它尚未设置?请参见上面的编辑,我希望我已经解释清楚了,但我不能给你一个答案,因为我没有足够的经验,在我看来,我不会在构造函数中手动更新对象,因为这样你就不知道对象是否通过实体框架代理,除非你每次需要访问它时都去检查ID,我的意思是你可以这样做,但我认为nk如果显式检查其是否为null,然后创建对象,那么会更安全、更清晰。在其他情况下,您可能会错误地覆盖当前对象,从而节省了自己的时间。您是否也可以包含json错误代码?
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;
    }