4

I'm currently working on trying to convert our companies framework from EF6 to be compatible with EF Core. I've run into a bit of a block. An EF "stored procedure" that works just fine on EF6 is now failing on this block of code in EF Core:

var allFolderAncestors = (from f in context.MENU_MenuFolders
                          from mtf in context.MENU_MenuToolbar_MenuFolders
                                             .Where(x => x.MenuFolderId == f.Id 
                                                      || x.MenuFolderId == f.ParentFolderId)
                                             .DefaultIfEmpty()
                          where (toolbarId == -1 
                          || (mtf == null 
                                ? false 
                                : mtf.MenuToolbarId == toolbarId)
                          ) 
                          && f.Id != 0
                          select new
                          {
                              AncestorFolderId = f.Id,
                              AncestorParentFolderId = f.ParentFolderId,
                              Id = f.Id,
                              ParentFolderId = f.ParentFolderId
                          }).ToList();

Trying to execute this line of code results in the following exception message:

Value cannot be null. Parameter name: left

In our .NET Core solution, this code DOES work when the input parameter toolbarId is set to -1. So my guess is the problem lies somewhere in the other side of the OR clause. And that's where I'm stuck. I was wondering if anyone has seen this problem before and knows how to fix it? Or how EF Core differs from EF6? I've tried several different fixes and looked in several places for a solution and have come up empty.

5
  • Are either MENU_MenuFolders or MENU_MenuToolbar_MenuFolders null when toolbarId != -1? Commented Mar 7, 2017 at 16:20
  • No, toolbarId == -1 is our default case, any positive value for toolbarId should "whittle down" the records returned so to speak. I've tried running the same query in LINQPad 4 and am having no issues there either.... Commented Mar 7, 2017 at 16:30
  • What was the EF version used in the working scenario (what you call .NET 4.5) - EF6 or EF Core? In other words, are you actually changing the EF? Commented Mar 7, 2017 at 16:39
  • In the working version we have EF6 installed, on our Core solution we are using the EF Core 1.1 package. I found a fix to the issue though Commented Mar 7, 2017 at 16:46
  • Then update the question and tags appropriately (if you want your post to be useful for other people). So the issue has nothing to do with .NET 4.5 or .NET Core, but EF Core (if you don't know, EF Core also supports Full .NET framework). Use entity-framework-core tag and replace .NET 4.5 with EF6 and .NET Core with EF Core inside the post. Commented Mar 7, 2017 at 17:05

1 Answer 1

4

Well, did a little bit more tinkering with it and found out what the issue was.

   var allFolderAncestors = (from f in context.MENU_MenuFolders
                             from mtf in context.MENU_MenuToolbar_MenuFolders
                                                .Where(x => x.MenuFolderId == f.Id 
                                                     || x.MenuFolderId == f.ParentFolderId)
                                                .DefaultIfEmpty()
                             where (toolbarId == -1 
                             || (mtf != null && mtf.MenuToolbarId == toolbarId)) 
                             && f.Id != 0
                             select new
                             {
                                 AncestorFolderId = f.Id,
                                 AncestorParentFolderId = f.ParentFolderId,
                                 Id = f.Id,
                                 ParentFolderId = f.ParentFolderId
                             }).ToList();

In the where clause after the OR, apparently .NET Core does not like the ternary operator. Hope this helps anyone else that may run into this issue.

Sign up to request clarification or add additional context in comments.

1 Comment

EF Core does not like the ternary operator - LOL. It's simply one (of the many) current EF Core bugs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.