2

I'm using below code to fetch latest inserted record:

public Request GetLastRequest()
{
    return _request.Find(_request.Max(p => p.Id));
}

As you can see Id is type of Guid:

public class Request
{
        public Request()
        {
            Id = Guid.NewGuid();
        }
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

}

After running my code, I'm getting this error:

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

PS: above code works if I use int for Id.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110
  • At which point you are getting Error? – Rahul Jan 03 '15 at 13:14
  • @Rahul Inside `GetLastRequest()` method. return – Sirwan Afifi Jan 03 '15 at 13:15
  • 2
    Guids are only meant to be unique, they are not meant to be ordered. You can not do Max on a Guid in SQL Server, thus it is not supported by EF. http://stackoverflow.com/questions/6069368/aggregate-function-on-uniqueidentifier-guid – juharr Jan 03 '15 at 13:22
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders Jan 24 '15 at 00:40

2 Answers2

4

You'll need to timestamp the record or put an auto-incrementing int on the record - GUIDs are generated randomly, so they won't actually represent a "max" GUID after insertion.

If your main intent is to get the record after inserting it, you could generate the GUID in your application layer and insert it into the database, this way you can return the GUID you just inserted in order to query back into the database for your information.

C Bauer
  • 5,003
  • 4
  • 33
  • 62
1

This works with int because entity framework can translate the Max()-linq-method to the appropriate sql-function. But it fails for Guid as they are not supported.

Take a look at this (scroll down to MAX( expression )): http://msdn.microsoft.com/en-us/library/bb399163%28v=vs.110%29.aspx

A Collection (T) where T is one of the following types: Byte, Int16, Int32, Int64, Byte, Single, Double, Decimal, DateTime, DateTimeOffset, Time, String, Binary.

Jan Köhler
  • 5,817
  • 5
  • 26
  • 35
  • This explains why the OP's code doesn't work, but doesn't really answer the question of how to get the latest record if Guids are used. – juharr Jan 03 '15 at 13:34
  • Sure, but I think the OP wasn't aware of this as he didn't ask for a solution but just wondered about the error. – Jan Köhler Jan 03 '15 at 13:43
  • I understand where you're coming from, but the title is literally "How can get the latest inserted record via Linq to Entities?". I just think your answer would be better if you included code for getting the last record inserted. – juharr Jan 03 '15 at 13:50