开发者

Why does an EntityCollection sometimes expose extension methods and sometimes not?

开发者 https://www.devze.com 2023-02-03 15:37 出处:网络
I\'m trying to make some changes to our Entity Framework model, which is causing me massive trouble at the moment - the idea is to change a 1:1 relationship between two databases into a many-many rela

I'm trying to make some changes to our Entity Framework model, which is causing me massive trouble at the moment - the idea is to change a 1:1 relationship between two databases into a many-many relationship. If you make the change in EF and then rebuild the database the object it generates to represent this relationship is - unsurprisingly - an EntityCollection instead of a single type object.

For the moment I've been running through all the errors that this change generates and altering DatabaseObject references to DatabaseObject.ElementAt(0) just so I can get it to build. However one set of references won't seem to giv开发者_C百科e me the extension methods on EntityCollection that allow one to operate on a collection - things like ElementAt(), Select(), First() and so on, and I can't see why.

In instances that work, the object is generated from a base class which is then inherited:

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

           //then in class which inherits above code

           string conStr = customerService.DatabaseObject.ElementAt(0).GetConnectionString(_customerService);

But in the instance that doesn't give me the extension methods, it's generated like this:

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

I can't see why these two instances are behaving differently?

Cheers, Matt


You need a using declaration for the namespace containing the extension method's classes.

E.g. to see Select, Where extension methods on IEnumerable<T> and IQueryable<T> there needs to be a

using System.Linq;

in your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消