为什么EntityCollection有时会暴露扩展方法,有时候不会?

Bob*_*way 2 .net entity-framework

我正在尝试对我们的实体框架模型进行一些更改,这会给我带来巨大的麻烦 - 我们的想法是将两个数据库之间的1:1关系转换为多对多关系.如果您在EF中进行更改然后重建数据库,则它生成的用于表示此关系的对象 - 不出所料 - 是一个EntityCollection而不是单个类型对象.

目前,我已经完成了此更改生成的所有错误,并将DatabaseObject引用更改为DatabaseObject.ElementAt(0),以便我可以构建它.然而,一组引用似乎没有给我EntityCollection上的扩展方法,允许一个人对一个集合进行操作 - 比如ElementAt(),Select(),First()等等,我看不出为什么.

在有效的实例中,对象是从基类生成的,然后继承该基类:

            _task = _customersRepository.GetDeepTask(taskId);
            _customerService = _task.CustomerServiceFeature.CustomerService;

           //then in class which inherits above code

           string conStr = customerService.DatabaseObject.ElementAt(0).GetConnectionString(_customerService);
Run Code Online (Sandbox Code Playgroud)

但是在没有给我扩展方法的实例中,它是这样生成的:

    public void Execute(ScheduledTask task)
    {
        CustomerService service = task.CustomerServiceFeature.CustomerService;
        //this errors and doesn't offer extension methods
        string ConnectionString = service.DatabaseObject.GetConnectionString(service);
    }
Run Code Online (Sandbox Code Playgroud)

我不明白为什么这两个实例表现不同?

干杯,马特

Ric*_*ard 5

您需要using包含扩展方法类的命名空间的声明.

例如看Select,Where扩展方法上IEnumerable<T>,并IQueryable<T>需要有一个

using System.Linq;
Run Code Online (Sandbox Code Playgroud)

在你的代码中.