CS Lewis
CS Lewis

Reputation: 489

The type or namespace name Edm could not be found are you missing a using directive or an assembly reference

All:

Details about technical environment used to develop ASP.NET application:

We have a class that contains the following Delete Method in :

public IHttpActionResult Delete([FromODataUri] Edm.Guid id){...}

Sadly, the aforementioned method Edm.Guid complains about the following error:

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

I can't remove the Edm because the aforementioned code is generated using T4 templates, and removing the Edm would be a pain.

In any case, could some please help me by telling me how the said code will work in such a way that Edm will be recognized?

--Update I found an unconventional solution which I dislike because it's kind of unsophisticated meaning it's like a hack.

In my T4 template, I have the following:

    var keyParameters = string.Join(", ", keys[entityName].Select(key => "[FromODataUri] " + (key.TypeUsage.ToString()).Substring((key.TypeUsage.ToString()).IndexOf(".") + 1).Trim()  + " " + code.Escape(key.Name).ToLower()));

...........
........
.....
public IHttpActionResult Delete(<#= keyParameters #>) {
...........
........
.....

It's Unsophisticated because I'm doing the weird String manipulation to get rid of anything prefixed with something like "Edm." :

   (key.TypeUsage.ToString()).Substring((key.TypeUsage.ToString()).IndexOf(".")

Please feel free to suggest a better alternative solution.

Upvotes: 0

Views: 917

Answers (1)

Mitch
Mitch

Reputation: 22251

It looks like you are trying to parse the OData type name. These are well-known and are usually parsed with a map from the name to the type, not by string manipulation. Something like the following would work:

var typeMap = new Dictionary<string, Type> 
{
    { "Edm.Binary", typeof(Byte[]) },
    { "Edm.Boolean", typeof(Boolean) },
    { "Edm.Byte", typeof(Byte) },
    { "Edm.DateTime", typeof(DateTime) },
    { "Edm.Decimal", typeof(Decimal) },
    { "Edm.Double", typeof(Double) },
    { "Edm.Single", typeof(Single) },
    { "Edm.Guid", typeof(Guid) },
    { "Edm.Int16", typeof(Int16) },
    { "Edm.Int32", typeof(Int32) },
    { "Edm.Int64", typeof(Int64) },
    { "Edm.SByte", typeof(SByte) },
    { "Edm.String", typeof(String) },
    { "Edm.Time", typeof(TimeSpan) },
    { "Edm.DateTimeOffset", typeof(DateTimeOffset) },
}

var edmTypeName = "Edm.Guid";
var typeName = "global::" + typeMap.SingleOrDefault(t => t.Key.Equals(edmTypeName, StringComparison.CurrentCultureIgnoreCase)).Value.FullName;
Debug.Assert(typeName == "global::System.Guid");

That being said, you could get clever by aliasing the System namespace. Adding using Edm = System; to the top of the template would work for most of the primitive types, but is a hack.

Upvotes: 1

Related Questions