0

The results from a stored proc are not mapping properly FOR GUIDs only (uniqueidentifiers in SQL)

menu.Sites = db.Database
.SqlQuery<Site>("get_application_sitemap @application_user_id, @application_name", 
new SqlParameter("application_user_id",id), 
new SqlParameter("application_name",applicationName)).ToList();

The Site class is shown below

 public class Site : EntityBase
{
    public Guid ApplicationWebpageGuid
    {
        get { return application_webpage_guid; }
    }

    public Guid application_webpage_guid { get; set; }

    public Guid? ParentApplicationWebpageGuid
    {
        get { return parent_application_webpage_guid ?? Guid.Empty; }
    }
    public Guid? parent_application_webpage_guid { get; set; }

    public string Name { get; set; }

    public string Url { get; set; }

    public int Children { get; set; }
}

The major issue is that the column names coming out of the stored proc are lower cased and underscore spaced. The way I am "mapping" and I use that term loosely is not something I like. The three properties Name, Url, and Children are mapping fine. The casing seems to have no effect, but the underscore spacing is causing me issues. Is there a way to map to a stored procedure? I can't make any changes to the stored proc without causing a major headache.

DeadlyChambers
  • 5,217
  • 4
  • 44
  • 61

2 Answers2

1

getting Entity Framework raw query to respect attributes

So you are running into the issue where EF is actually ignoring the attributes that you are putting on the column. It doesn't look like they fixed it in EF 7 so good luck.

Community
  • 1
  • 1
jay
  • 26
  • 3
0

In entity framework you can map column names using FluentAPI like this:

modelBuilder.Entity<Site>() 
            .Property(t => t.ParentApplicationWebpageGuid) 
            .HasColumnName("parent_application_webpage_guid");

Or with data annotations like this:

[Column(“parent_application_webpage_guid")] 
public String ParentApplicationWebpageGuid {get;set;}

References:

Mapping a CLR Property to a Specific Column in the Database

Mapping Tables And Columns using Data Annotations

Colin
  • 22,328
  • 17
  • 103
  • 197
  • I have tried the data annotations, but I will give the mapping a shot. – DeadlyChambers Feb 06 '15 at 15:28
  • Scratch that, just realised what's going on. From documentation for Database.SqlQuery - it "creates a raw SQL query that will return elements of the given generic type. The type can be any type that has properties that **match the names of the columns** returned from the query." – Colin Feb 06 '15 at 15:36
  • That is probably why the other three are working. But I want to map the columns to properties on the Site object without the underscores. I am trying to use the map column technique right now and it is yelling about "ApplicationWebpageGuid doesn't have a corresponding column in the data reader with the same name." Right now I am trying to get to the bottom of this one. – DeadlyChambers Feb 06 '15 at 15:55