none
while quering sqlserver using entity framwork 5 i am getting an error. RRS feed

  • Question

  • hi,

    vs 2012 sqlserver 2005

    create table abc ( col1 datetime,col2 decimal(10,4))

    select col1,count(col2) as col2 from abc  group by col1

    i am sending this query using dbcontext.database.sqlquery()

    but i am getting error becasue count returns int where as col2 is decimal(10,4) in database and in entity

    please tel me is there any solution.

    yours sincerely

    Saturday, September 21, 2013 1:36 PM

Answers

  • Hi rajemessage;

    Well we will give it a try. You state in your question the following, "i am sending this query using dbcontext.database.sqlquery(), but i am getting error becasue count returns int where as col2 is decimal(10,4) in database and in entity", the T-SQL Count(...) returns the number of items in a group and is of type integer. Count does NOT Sum the column and therefore does not need and is not of the Type of col2 in this case decimal(10,4). 

    In your follow up question you state the following, "this is function i am using, with entity haveing tables from database.", the type of the entity is that which is being returned to the caller. Therefore the return type must have at lease two public properties having the same column names as the select statement. For example from the initial question your select is, select col1, count(col2) as col2, and therefore the entity return type must have the following:

    public class EntityReturnTypeName
    {
        public col1Type col1 { get; set; }
        public int col2 { get; set; }
    }  

    Where col1Type is the CLR data type that is a match for the SQL data type for col1.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, September 23, 2013 4:19 PM

All replies

  • Hi rajemessage;

    Please post the actual code by copying it from your project and pasting here. When pasting code here please use the code icon which is the second icon from the right on the edit tool bar.

    Also show the line the error happens on and the exact exception message and inner exception message if any.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Saturday, September 21, 2013 2:23 PM
  • due to company policy i can not provide code,

    so please ask me perticualar things which u want, i can provide it.

    this is function i am using, with entity haveing tables from database.

    SqlQuery(Type, String, Object[])

    yours sincerly.

    Monday, September 23, 2013 7:43 AM
  • Hi rajemessage;

    Well we will give it a try. You state in your question the following, "i am sending this query using dbcontext.database.sqlquery(), but i am getting error becasue count returns int where as col2 is decimal(10,4) in database and in entity", the T-SQL Count(...) returns the number of items in a group and is of type integer. Count does NOT Sum the column and therefore does not need and is not of the Type of col2 in this case decimal(10,4). 

    In your follow up question you state the following, "this is function i am using, with entity haveing tables from database.", the type of the entity is that which is being returned to the caller. Therefore the return type must have at lease two public properties having the same column names as the select statement. For example from the initial question your select is, select col1, count(col2) as col2, and therefore the entity return type must have the following:

    public class EntityReturnTypeName
    {
        public col1Type col1 { get; set; }
        public int col2 { get; set; }
    }  

    Where col1Type is the CLR data type that is a match for the SQL data type for col1.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, September 23, 2013 4:19 PM