none
Compiled query bug? RRS feed

  • Question

  • I ran into a strange issue,

     

    I have a function for password reset, first time the function is called everything goes as expected, but any subsequent call no updates are sent to the sql server.

     

    I use the same email in both cases, linq also finds the same LegalEntity in both cases.

    If I inspect the result from db.GetChangeSet(), first time it recognize that an update has been made, subsequent calls dont, even if i see that the Password property of the LegalEntity has changed.

     

    I can imagine that it has to do with that the LegalEntity in this case is selected by using a compiled query, since it is static it seems to fit the scenario? It feels like linq somehow keeps track of what is updated, and doesn't reset even if I create a new data context.

     

    LegalEntity legalEntity;

    string newPassword;

    using (SampleDataContext db = new SampleDataContext(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString))

    {

    legalEntity = MemberQueries.GetUserByEmail(db, email);

    if (legalEntity == null)

    {

    return ResetPasswordResult.InvalidEmail;

    }else

    {

    newPassword = ServiceHelper.GenerateRandomString(8, false);

    legalEntity.PasswordMD5Hash = ServiceHelper.GetMD5Hash(newPassword);

    db.SubmitChanges();

    }

    }

     

    if i change

    legalEntity = MemberQueries.GetUserByEmail(db, email);

    to

    legalEntity = db.LegalEntitys.FirstOrDefault<LegalEntity>(l => l.Email == email);

    Everything works fine,

     

    the compiled query looks like this:

    public static Func<SampleDataContext, string, LegalEntity>

    GetUserByEmail = CompiledQuery.Compile

    ((SampleDataContext context, string email) =>

    context.LegalEntitys.FirstOrDefault<LegalEntity>(m => m.Email == email));

     

    It would be weird if this was a bug so it's probably something im not aware of.

    Anyone care to shed some light on this? 

     

    Thanks,

    /Micke

    Sunday, November 23, 2008 12:06 AM

Answers

  • I tried to reproduce your example using Northwind database.  The following code seems to work correctly. 

     

    class Program

    {

      public static void Main(string[] args)

      {

        while (true)

        {

          using (NorthwindDataContext db = new NorthwindDataContext())

          {

            Customer cust = GetCustomerByID(db, "ALFKI");

            cust.ContactName = cust.ContactName + "X";

            var cs = db.GetChangeSet();

            Console.WriteLine("Updated: {0}", cs.Updates.Count);

          }

        }

      }

      public static Func<NorthwindDataContext, string, Customer> GetCustomerByID =

        CompiledQuery.Compile((NorthwindDataContext dc, string id) =>

        dc.Customers.FirstOrDefault(c => c.CustomerID == id)

        );

    }

     

    It should print "Updated: 1" everytime through the loop.

    Does it work for you or fail the same as your example?

    Sunday, November 23, 2008 5:54 PM
    Moderator