Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';保存新记录后不延迟加载实体_C#_Entity Framework_Lazy Loading_Poco - Fatal编程技术网

C# Can';保存新记录后不延迟加载实体

C# Can';保存新记录后不延迟加载实体,c#,entity-framework,lazy-loading,poco,C#,Entity Framework,Lazy Loading,Poco,我在MVC 3.0应用程序中使用Entity Framework 4.1。该应用程序是一种n层方法,我的UI、模型、类、服务和存储库都放在一个解决方案中的单独项目中。我还使用工作单元方法和依赖项注入,因此,我无法访问UI中的DbContext 我有两门课,如下所示 public partial class Form { public int id { get; set; } public int eventID { get; set; } public string sa

我在MVC 3.0应用程序中使用Entity Framework 4.1。该应用程序是一种n层方法,我的UI、模型、类、服务和存储库都放在一个解决方案中的单独项目中。我还使用工作单元方法和依赖项注入,因此,我无法访问UI中的DbContext

我有两门课,如下所示

public partial class Form
{
    public int id { get; set; }
    public int eventID { get; set; }
    public string sampleName { get; set; }

    public virtual Event GetEvent{ get; set; }
}

public partial class Event
{
    public int id { get; set; }
    public string eventName { get; set; }
}
然后在我的控制器中,我添加了一个类表单的实例,如下所示

Form _form = new Form();
_form.eventID = 3;
_form.sampleName = "myString";

_formService.AddForm(_form);
_formService.SaveChanges(); //This calls the Unit of Work Commit
string _eventName = _form.GetEvent.eventName;
这将起作用并将记录插入数据库。但是在SaveChanges()调用下面的几行中,我尝试像这样延迟加载相关的事件类

Form _form = new Form();
_form.eventID = 3;
_form.sampleName = "myString";

_formService.AddForm(_form);
_formService.SaveChanges(); //This calls the Unit of Work Commit
string _eventName = _form.GetEvent.eventName;
但是值
\u form.GetEvent
始终为空。看起来
GetEvent
尚未加载

有人能帮我吗


提前感谢。

尝试将
GetEvent
属性重命名为
Event
,以便EF可以按照通常的方式推断外键关系(通过
eventId
)。

您应该访问上下文,并创建一个代理而不是新表单,这样您就可以在存储库中编写一个方法来访问上下文,返回类型为表单类

有关更多信息,请参见此