Question

I'm using Entity Framework 4.3. In my SQL Server 2008 db, I have some dates stored as dates rather than datetime.

I need to retrieve data using the date field, here a test code sample:

    public static Applicant Create()
    {   
        var dt = new DateTime(1967, 08, 03);

        var r = new CrudRepo<Applicant>(UowHelper.GetUow().Context);

        return r.Find(a => a.DateOfBirth == dt).Single();
    }

However, I'm getting an issue with 'missing sequence'.

Any ideas as to how I get around this?

I'll also need to update the database too at some point.

Thanks.

Was it helpful?

Solution

The actual result of var dt = new DateTime(1967, 08, 03); is 8/3/1967 12:00:00 AM

The DataType of the DateOfBirth is Date so we need to TruncateTime the time.

msdn: EntityFunctions.TruncateTime Method

public static Applicant Create()
    {   
        var dt = new DateTime(1967, 08, 03);

        var r = new CrudRepo<Applicant>(UowHelper.GetUow().Context);

        return r.FirstOrDefault(a => a.DateOfBirth == EntityFunctions.TruncateTime(dt));
    }

or remove the .Single() method instead use the .FirstOrDefault() Method

OTHER TIPS

FYI, to add to spajce's answer,

System.Data.Entity.Core.Objects.EntityFunctions

has been deprecated to

System.Data.Entity.DbFunctions
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top