1

我正在尝试强制实体框架 5.0 在我生成的每个 poco 类上创建一个构造函数。这个构造函数应该实例化我拥有的任何外键导航属性。

例如

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 为空,我得到一个解析错误。我可以在每个事件的代码中设置它,但这并不聪明。如果可能的话,我需要一个链接或一个例子来帮助实现我所需要的。

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;
    }
4

0 回答 0