【问题标题】:Model not updating with new values模型未使用新值更新
【发布时间】:2018-10-30 12:32:10
【问题描述】:

在应用程序运行时更改值后,我遇到用户模型未使用正确的 ParentAccount ID 更新的问题。

举例

我将运行应用程序并更改联系人父帐户。然后创建一个订单并使用下面的“创建”方法将父帐户分配给该订单。

现在我觉得它应该运行“GetUser”方法来更新当前用户模型,然后获取当前用户并将其分配给销售订单。

相反,它会跳过它并首先运行它下面的代码,并且永远不会使用正确的父帐户 ID 更新它。

有人对为什么会发生这种情况有任何建议吗?

谢谢!

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();
}

这是用户模型

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;}

}

这里是创建用户模型的地方

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;

}
}

【问题讨论】:

    标签: c# asp.net-mvc dynamics-crm


    【解决方案1】:

    这可能是因为您在两次调用之间重复使用了 CrmContextCore 对象。第一次调用时,上下文会添加和跟踪用户记录,然后后续调用实际上不会重新查询 CRM,而是从跟踪对象返回旧数据。

    这是同一个问题,尽管场景不同,如这个问题Getting weird behavior when retrieving data from Microsoft CRM using LINQ

    【讨论】:

    • 我使用了 ClearChanges 方法,这对我有用。谢谢!
    【解决方案2】:

    没有看到模型定义,只能这样回答,和Entity Framework有关:

    如果您需要从数据库表中的自动增量字段(模型映射到该字段)返回更新/新的 ID 值,则使用以下内容装饰模型字段:

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    

    这将允许新的 ID 值在模型收到来自 EF 的更新/创建信号后可用。

    【讨论】:

    • Willem 我更新了我的代码以包含使用动态 crm sdk 检索用户的调用。我没有使用实体框架。
    【解决方案3】:

    您正在创建一个新实体,并从您从数据库中检索到的实体分配值。如果要更新 DB 实体,则需要更新 DB 实体上的字段,或者可以将新实体附加到数据上下文。看到这个帖子Why use Attach for update Entity Framework 6?

    【讨论】:

    • 我更新了我的代码以包含使用 Dynamics CRM SDK 检索用户的调用。你的例子还有意义吗?因为我必须在运行 GetUser 方法后手动更新 EntityReference?
    • 我没有使用过那个 SDK,但它应该可以这样工作,因为 EF 就是这样工作的。我刚刚进行了快速搜索,链接答案中的示例仍然密切相关。
    猜你喜欢
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    相关资源
    最近更新 更多