使用Database First方法选择性禁用延迟加载 [英] Selective disabling of lazy loading with Database First method

查看:119
本文介绍了使用Database First方法选择性禁用延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想在整个上下文中禁用延迟加载。我只想选择性地禁用一些关键导航属性的延迟加载。

I am not interested in disabling lazy loading for the entire context. I want only to selectively disable lazy loading for a few key navigational properties.

使用首先方法,我了解如何有选择地禁用懒惰加载:

With the Code First method, I understand how to selectively disable lazy loading:

public virtual Person Requestor { get; set; } //lazy loading enabled
...
public Person Requestor { get; set; } //lazy loading disabled

但是使用数据库优先方法,是从模板生成的代码,所以这种修改将在下一次重新生成时丢失。

However with the Database First method, this is code generated from a template so this modification is going to be lost on the next regeneration.

有没有办法修改模型或模板生成器,以便选择性禁用懒惰加载?

Is there a way to modify the model or template generator for such selective disabling of lazy loading?

推荐答案

我不知道一种方式。但是,如果要使用实体代码生成器,您可以构建一个警告系统,以便在重新生成代码时立即收到通知(取决于您的构建策略)。

I don't know of a way. But if you are going to use the Entity code generator you could build in a warning system so that when the code is re-generated you get notified immediately (depending on your build strategy).

所以我会做的是对于所选择的实体,说实体是请求,并且所涉及的属性是请求者然后写一个测试来声明该属性不是虚拟的

So what I would do is for the selected entites, say the entity is Request and the property in question is Requestor then write a test to assert that the property is NOT virtual

            [TestMethod()]
            public void RequestPropertyRequestor_MustNotBeVirtual() {

                PropertyInfo[] properties = typeof(Request).GetProperties()
                    .Where(p => p.GetGetMethod().IsVirtual).ToArray();
               Assert.AreEqual(0, properties.Count(p => p.Name == "Requestor"), "Model Code Regenerated - change the Request Entity");
            }

不知道反射代码的准确性,但是你得到我的意思。这样当实体被重新生成并且您修改了代码时,测试失败。预警系统

Not sure of the accuracy of the reflection code but you get what i mean. This way when the entities are regenerated and you have amended the code, the test fails. early warning system

您可以关闭代码生成并使用POCO。

you could turn off code generation and use POCO's.

推荐更改

如果您不想关闭代码,那么修改T4模板就是去的方式只需

If you don't wanna turn off code gen then modifying the T4 template is the way to go. Just


  • 将代码生成策略设置为 EDMX设计师使默认生成不发生。这导致在EDMX设计器中没有导出 DbContext 或实体类

  • ,右键单击绘图面,然后选择添加代码生成项目。在那里应该有发电机,如果不是只是通过NuGet安装一个。选择EF5 DbContext。

  • 查找实体生成和修改的T4模板。

  • set the "Code Generation Stategy" to None in the properties of the EDMX designer so that the default generation doesn't occur. This results in no derived DbContext or entity classes
  • in the EDMX designer, right click on the drawing surface and select "Add Code Generation Item". There should be generators listed there, if not just install one through NuGet. Select the EF5 DbContext one.
  • Find the T4 template for the entity generation and modify.

这篇关于使用Database First方法选择性禁用延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆