0

I am having an issue with a user model not updating with the correct ParentAccount ID after changing the value while the application is running.

For Example

I will run the application and change the contacts parent account. Then create an order and use the "Create" method below to assign the parent account to the order.

Now I feel it should run the "GetUser" method which updated the currentuser model and then takes current user and assign it to the salesorder.

Instead its like it skips that and runs the code below it first and never updates it with the correct Parent Account Id.

Does anyone have any suggestions on why this would happen?

Thanks!

public void Create(CrmContextCore _crmContext, Guid productId, ClaimsPrincipal User)
{
    // User Model 
    UserEntityModel currentuser;

    DAL_UserEntity UserData = new DAL_UserEntity();


    var EmailAddress = User.Claims.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.Email)?.Value;

    var salesorder = new Entity("salesorder");
    {
        // Go get the current user data from crm system

        currentuser = UserData.GetUser(_crmContext, EmailAddress);

        // ISSUE! If i change this value while the application is running and rerun the method it shows the old value of currentuser not the new one??

        salesorder["customerid"] = new EntityReference("account", currentuser.ParentAccount.Id);
        salesorder["contactid"] = new EntityReference("contact", currentuser.ContactId);
        salesorder["emailaddress"] = currentuser.Email;
        salesorder["name"] = "PO123";
    }

    _crmContext.ServiceContext.AddObject(salesorder);

    _crmContext.ServiceContext.SaveChanges();
}

Here is the User Model

public class UserEntityModel
{

    public Guid ContactId {get; set;}
    public EntityReference ParentAccount { get; set; }
    public Guid Account {get; set;}
    public string FirstName {get; set;}
    public string LastName {get; set;}
    public string Email {get; set;}

}

Here is where the User model is created

public class DAL_UserEntity
{
public UserEntityModel GetUser(CrmContextCore _crmContext, string email)
{

    Console.WriteLine("GetUser Method is Running!!");

    var user = (from u in _crmContext.ServiceContext.CreateQuery("contact")
                where u.GetAttributeValue<string>("emailaddress1") == email
                select u).Single();

    UserEntityModel ctx = new UserEntityModel();

    ctx.FirstName = user.GetAttributeValue<string>("firstname");
    ctx.LastName = user.GetAttributeValue<string>("lastname");
    ctx.Email = user.GetAttributeValue<string>("emailaddress1");
    ctx.ContactId = user.GetAttributeValue<Guid>("contactid");
    ctx.ParentAccount = user.GetAttributeValue<EntityReference>("parentcustomerid");

    return ctx;

}
}
dbollig
  • 81
  • 4

3 Answers3

0

Without seeing the model definitions, I can only answer it like this and related to Entity Framework:

If you need the updated / new ID value to be returned from something like an autoincrement field in the database table, to which the model is mapped, then decorate the model field with:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

This will allow the new ID-value to be available after the model has received the update / create signal from EF.

  • Willem I updated my code to include the call to retrieve the user using dynamics crm sdk. I am not using the entity framework. – dbollig Oct 30 '18 at 13:45
0

You are creating a new entity, and assigning values from the entity you retrieved from the DB. If you want to update the DB entity you need to update the fields on the DB entity, or you can attach the new entity to the data context. See this post Why use Attach for update Entity Framework 6?

crunchy
  • 705
  • 1
  • 13
  • 35
  • I updated my code to include the call to retrieve the user using the Dynamics CRM SDK. Would your example still be relevant? As I would i have to manually update the EntityReference after running the GetUser method? – dbollig Oct 30 '18 at 13:44
  • I haven't worked with that SDK but it should work like that as that's how EF works. I just did a quick search and the example in the linked answer it still germane. – crunchy Oct 30 '18 at 13:52
0

It is likely because you are re-using the CrmContextCore object between both calls. The first time you call it the User record is added and tracked by the context, and then subsequent calls do not actually re-query CRM, instead returning the old data from the tracked object.

It is the same issue, although a different scenario, as in this question Getting weird behavior when retrieving data from Microsoft CRM using LINQ

Ben Williams
  • 1,193
  • 1
  • 10
  • 22