1

I have model generated by ADO Entity Data Model like:

public partial class Category
{
    public Category()
    {

    }    
    public int CategoryId { get; set; }
    public string Name { get; set; }
}

now I have added one property

public partial class Category
{
    public int EventsCount { get; set; }
}

Now I am trying to map by: //this is working Query

List<Category> retVal = db.Database.SqlQuery<Category>(
                //retVal = db.Categories.SqlQuery(
                        @"SELECT c2.CategoryId,c2.Name,c1.EventsCount AS EventsCount FROM (
                        SELECT c.CategoryId, COUNT(c.CategoryId) AS EventsCount FROM Category c
                        JOIN EventCategory ec ON ec.CategoryId = c.CategoryId
                        JOIN (SELECT * FROM EVENT WHERE EventDateTime > DATEADD(D, 0, DATEDIFF(D, 0, GETDATE()))) e ON e.EventId = ec.EventId
                        WHERE c.ImportedFrom IS NULL                    
                        GROUP BY c.CategoryId) c1
                        join Category c2 ON c1.CategoryId = c2.CategoryId").ToList();

Actual Data I get executing raw sql on Management Studio

enter image description here

but EventCounts is always 0 with Entity Framework mapping ,

but If I map model with different Model having EventsCount , then it is mapped.

like:

public partial class Category
    {
        public Category()
        {

        }    
        public int CategoryId { get; set; }
        public string Name { get; set; }
        public int EventsCount { get; set; }
    }

now there are mapped all columns,, Any idea here, why Entity framework in not mapping partial model?

Mujah Maskey
  • 8,654
  • 8
  • 40
  • 61

3 Answers3

2

I have not got exact answer for this, But I got solution by inheriting model than making partial class. As EntityFramework mapp data with help of reflection and reflection cannot deal with property with partial class

check this .NET reflection: how to get properties defined on partial class

so what I did is: first make anther class with inheriting class generated by EF, and add property that we want.

public partial class CategoryEx:Category
{
    public int EventsCount { get; set; }
}

and definitely,

List<CategoryEx> retVal = db.Database.SqlQuery<Category>(
                //retVal = db.Categories.SqlQuery(
                        @"SELECT c2.CategoryId,c2.Name,c1.EventsCount AS EventsCount FROM (
                        SELECT c.CategoryId, COUNT(c.CategoryId) AS EventsCount FROM Category c
                        JOIN EventCategory ec ON ec.CategoryId = c.CategoryId
                        JOIN (SELECT * FROM EVENT WHERE EventDateTime > DATEADD(D, 0, DATEDIFF(D, 0, GETDATE()))) e ON e.EventId = ec.EventId
                        WHERE c.ImportedFrom IS NULL                    
                        GROUP BY c.CategoryId) c1
                        join Category c2 ON c1.CategoryId = c2.CategoryId").ToList(); 

now EventsCount is there.

I have no idea but it may be helpful for somebody.

Mujah Maskey
  • 8,654
  • 8
  • 40
  • 61
  • I have mark this as answer, because this solves my problem but not correct answer yet. and I will mark other as answer on future, if anybody has great idea. – Mujah Maskey Jul 18 '13 at 13:34
0

As far as my interpretation is concerned you are trying to bind more data then it is there in the class. You can bind only event count rather then trying to bind c2.CategoryId, c2.Name using select query. I am not sure but i think that this should work.

Nisarg Shah
  • 354
  • 1
  • 14
  • 1
    little disagree with your answer, it seems like adding one more Property in generated Model. And as I understand you, you are saying we can use Count/Count() prop/method four counting right? we shouldnot use that in this case I guess, because it will fetch all un-necessary data.. Please check Intellitrace for both idea and run that query alone on ManagementStudio, there will be very unnecessary data fetched which is generated by Count prop. This is the reason, I am fetching EventCounts only , not all Events and Count later. – Mujah Maskey Jul 18 '13 at 12:12
0

I came across the same issue . It seems to be a bug in EF. For me the solution was the excellent Dapper library. It's just a bunch of extension methods so it can work side by side with EF.

Pawel
  • 1
  • 1