0

Casting a value in the extension method and the function in the edmx file is casting a value from a string to what we want.

That's fine as long as I would know what the a.Rating.RatingValue and c.RatingId values are ahead of time. They are not hardcoded, nor do I know what the values would be ahead of time.

UNfortunately, the first value is a Byte and the second value is an Int32.

So, in my extension methods, I have to set them up as follows. Note that Object.Parse(value) only works if the value is a string, so I can't do it like that.

With the below code, I'm still getting the same error message and is very frustrating. I don't know what else I need to do in order for it to convert successfully.

I have a requirement to get this in by tomorrow. If anyone could be of help, I would appreciate it.

Here is my query:

var restaurants = await DbContext.Restaurants.GroupBy(g => g).Select(s =>
                        new RestaurantList()
                        {
                            Name = s.Key.Name,
                            City = s.Key.City,
                            Phone = s.Key.Phone,
                            AvgRating = s.Average(a => EdmxExtensionMethods.ParseDecimal(a.Rating.RatingValue)),
                            NbrOfPeopleRating = s.Distinct().Count(c => EdmxExtensionMethods.ParseBoolean(c.RatingId)),
                            Id = s.Key.Id
                        }).ToListAsync();

                    return restaurants;
                }

Here are my extesnion methods. Again this class is outside the EDMX file on the root of the project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace BillYeagerDB
{
    public partial class EdmxExtensionMethods : DbContext
    {
        [DbFunctionAttribute("BillYeagerDB", "ParseDecimal")]
        public static Decimal ParseDecimal(Int16 int16value)
        {
            return Convert.ToDecimal(int16value);
        }

        [DbFunctionAttribute("BillYeagerDB", "ParseBoolean")]
        public static bool ParseBoolean(Int32 intvalue)
        {
            return Convert.ToBoolean(intvalue);
        }
    }
}

These are my functions inside the EDMX file:

<edmx:ConceptualModels>
      <Schema Namespace="BillYeagerModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
        <Function Name="ParseDecimal" ReturnType="Edm.Decimal">
          <Parameter Name="bytevalue" Type="Edm.Byte" />
          <DefiningExpression>
            cast(bytevalue as Edm.Decimal)
          </DefiningExpression>
        </Function>
        <Function Name="ParseBoolean" ReturnType="Edm.Boolean">
          <Parameter Name="intvalue" Type="Edm.Int32" />
          <DefiningExpression>
            cast(intvalue as Edm.Boolean)
          </DefiningExpression>
        </Function>
sagesky36
  • 4,542
  • 19
  • 82
  • 130
  • 1
    yet another very popular Entity Framework related exception. – King King Nov 25 '13 at 19:55
  • You appear to have a table called `Restaurants` that has both restaurants and their reviews. This is a bad sort of a non-[normalized DB](http://en.wikipedia.org/wiki/Database_normalization), and should be fixed (though I don't think it's related to your issue). – Tim S. Nov 25 '13 at 19:56
  • What is the error message? I don't think you told us... – Tim S. Nov 25 '13 at 19:57
  • @TimS. the error message is in the title, and the *strange* methods come from this extension `EdmxExtensionMethods.` I think. – King King Nov 25 '13 at 20:23
  • Why do you ask [again](http://stackoverflow.com/q/20182823/861716)? – Gert Arnold Nov 25 '13 at 20:55
  • Because I didn't get any valid feedback from the first question. Nothing to say what I'm doing wrong and how to fix it... – sagesky36 Nov 25 '13 at 21:26
  • 1
    Well, you should _never_ re-post a question. You should have improved the first one by cutting it back to the essence. If you edit a question it will be bumped back into attention. Maybe you should hang to this question now, although I don't understand your latest edit. Now things don't match anymore. – Gert Arnold Nov 25 '13 at 21:31
  • The issue was I needed to change my namespace that is referenced in the edmx file from "BillYeagerDB" to "BillYeagerModel". However, even after making that change, I got another error which is a bug in EF 6.01 at this link, so I can't do anything about it now. http://entityframework.codeplex.com/discussions/470366 – sagesky36 Nov 25 '13 at 21:49

0 Answers0