0

I am maintaining a visual studio 2008 application (website project). There is no chance to upgrade to higher version of.net framework or upper version of entity framework. Like many IT shops, unless there are major issues, people are not going to allow me to do major upgrade on the system.

The .net framework is 3.5. and the EF version is 1.0

I need to change my program, so my select linq statement will work

Calling a SQL User-defined function in a LINQ query

As you can tell, I need to include stored function as a part of select statement

So I copied the statement.

I have struggled for hours, and I keep getting compilation.

//..various using statement
using System.Data.Objects.DataClasses;
using System.Data.Metadata.Edm;

//..other class
public static class EntityFunctions 
{
    [EdmFunction("FBLModel.Store", "SampleFunction")]
    public static int SampleFunction(int param)
    {
        throw new NotSupportedException("Direct calls are not supported.");
    }
}

I keep getting compilation errors

error CS0246: The type or namespace name 'EdmFunctionAttribute' could not be found (are you missing a using directive or an assembly reference?)

I have searched the whole internet include stackoverflow and MSDN blog, the namespace looks correct

enter image description here

Any suggestions? Thank you

user12345
  • 181
  • 1
  • 2
  • 18
  • You're exception indicates that nothing should directly call the method. This makes me suspect no methods directly call the method or possibly even the whole library and so the compiler doesn't include the reference when it builds to solution since it thinks it's "optimizing" output by ignoring the unused reference. Can you try creating a hard reference so you know for certain that the compiler isn't ignoring it? e.g. Create a public property called test, then get the value in your main project once, just so the code touches it at least once and ensure the assembly is loaded? – Joe_DM Jul 15 '17 at 06:41
  • above i say the compiler ignores it, but that's not accurate since the dll will still be included in the output. It's more of a runtime optomization that I'm talking about where the assembly is never loaded. Just wanted to clarify that :) – Joe_DM Jul 15 '17 at 06:47
  • It could be a problem with the T4 templates. Perhaps they belong to the wrong EF version? Go to `Model.Context.tt` and check that the `using System.Data.Objects.DataClasses;` is included. I got the hint from [this post](https://social.msdn.microsoft.com/Forums/en-US/2c8143eb-f7cd-48d1-b67d-9789acd9e152/type-or-namespace-edmfunctionattribute-could-not-be-found-entity-framework-601?forum=adodotnetentityframework). – Diana Jul 16 '17 at 16:36
  • It's a typical trap to think that solving issues time and again is cheaper than upgrading. It's a logical flaw (just read a bit in *Thinking, Fast and Slow*, Kahneman). Besides, EF 1 is notoriously immature, it's hardly used anymore, so you're virtually on your own. Also, it's an illusion to think that you're going to upgrade when there are major issues. The best time to upgrade (and regression test) is when there are NO issues. – Gert Arnold Jul 16 '17 at 22:17
  • Thanks for everyone's response. It does not context.tt. Strange. It has dbml. I can no longer using gui. I have to go to file to change manually. This is the second in my career I have to do it. I inherit. There is not much I can do. I have 4 projects to do. For this old project, I just have to make sure it does not break. Eventually, it will upgrade. Right, it is not a right time BC I got no time. Oh, this is website project. I need to figure how it is reference dll. I really want to K... the original developer. I did 2008 b4. It was not like this! – user12345 Jul 17 '17 at 14:31
  • So you're mixing up LINQ-to-SQL (what you have now) and Entity Framework (what you're trying). You should add the function to the DBML file. Just experiment a bit with an existing database and a new dbml to see the required syntax. – Gert Arnold Jul 19 '17 at 22:31

1 Answers1

0

The problem here is that you have a conflicting reference with your namespaces.

The System.Data.Metadata.Edm namespace contains a class called EdmFunction. The System.Data.Objects.DataClasses namespace contains a class called EdmFunctionattribute (which is the one you are trying to use).

For some reason your code is referencing the EdmFunction class. Try removing the namespace import for System.Data.Metadata.Edm, as you probably didn't want to import this in the first place.

rozza2058
  • 560
  • 1
  • 3
  • 11