ktutnik
ktutnik

Reputation: 7260

Entity Framework - failed cast string to datetime on exec stored procedure

So, I am really new to EF, an the problem a bit complicated for me.

Here is the data returned by my stored procedure:

[Date]     [Name]
8/31/2012  John Doe
8/12/2011  John Meyer

My entity is:

public class UserEntity
{
   public DateTime Date {get; set;}
   public string Name {get; set;}
}

when I change Date into string it is working fine.

I used

Database.SqlQuery<UserEntity>("exec sp_name", /*params*/)

to get the list.

Another problem that make it more complicated: stored procedure and entity are legacy code, used in different project so it is impossible to change.

Is there any chance for me to solve the case?

Upvotes: 0

Views: 812

Answers (1)

Eren Ers&#246;nmez
Eren Ers&#246;nmez

Reputation: 39085

One way would be to define another class to retrieve the result:

public class Result
{
   public string Date {get; set;}
   public string Name {get; set;}
}

and then convert it to UserEntity:

var users = Database.SqlQuery<Result>("exec sp_name", /*params*/)
    .Select(x=> new UserEntity
    {
        Date = DateTime.Parse(x.Date),
        Name = x.Name
    });

Upvotes: 1

Related Questions