0

I have a mysterious issue.

I use Linq to Entities to get a user profile and send the result object to a function to fill it with MVC model data.

using (var db = new DatabaseEntities())
{
    try
    {
        var user = db.Users.Single(u => u.Id == accorJobPrincipal.Id);
        //At the point, user.CreatedAt contains the right date
        user = model.GetBaseObject(user);
        //At the point, user.CreatedAt is equal to DateTime.MinValue (0001/01/01)

        user.UpdatedAt = DateTime.Now;

        db.SaveChanges(); //Here, I have an exception due to Overflow in SQL Datetime convert
    }
    catch (Exception ex)
    {
        ModelState.AddModelError(string.Empty, "A system error occured, please try later.");
    }
}

Why the entity object lost data when it passed as parameter ?


EDIT Please find here the code of GetBaseObject()

public override Users GetBaseObject(Users objectFromContext = default(Users))
    {
        var returnUser = objectFromContext ?? new Users();

        returnUser.FirstName = FirstName;
        returnUser.LastName = LastName;
        returnUser.IdTitle = IdTitle;
        returnUser.CreatedAt = CreatedAt;
        returnUser.UpdatedAt = UpdatedAt;
        returnUser.DeletedAt = DeletedAt;
        returnUser.LastConnectionAt = LastConnectionAt;
        returnUser.Enabled = Enabled;
        returnUser.Email = Email;
        returnUser.NTLogin = NTLogin;
        returnUser.Login = Login;
        returnUser.Password = Password;
        returnUser.PasswordAttemptFailCount = PasswordAttemptFailCount;

        return returnUser;
    }
3
  • Potentially your SQL Server DateTime settings are different to your .NET environment. I.e. one is US and one UK
    – ediblecode
    Mar 20, 2015 at 11:30
  • What does GetBaseObject do? Quick search of the net gives me nothing.
    – kjbartel
    Mar 20, 2015 at 11:35
  • Please provide the code for "GetBaseObject" function
    – Sumit
    Mar 20, 2015 at 12:19

1 Answer 1

0

I'm sorry, I've found my noob error posting the code :-(

I rewrite the value of dates during GetBaseObject() ....

It useful to post the code here ^^ you see the errors very well. Better than in my Visual Studio :-P that I used every time.

Thanks all to your participation

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.