[英]Dynamics CRM 2016 c# use id of not yet existing entity
for my project, I have to create multiple Quotes and add products to it. 对于我的项目,我必须创建多个报价并向其中添加产品。
For performance reasons (about 5000 quotes) I am using "ExecuteMultipleRequest()". 出于性能原因(大约5000个引号),我使用“ ExecuteMultipleRequest()”。
This is about what I've got: 这是关于我所拥有的:
var quote = new Quote
{
QuoteNumber = "123",
Name = "test123",
PriceLevelId = new EntityReference(PriceLevel.EntityLogicalName, Pricelevel.Id),
CustomerId = new EntityReference(Account.EntityLogicalName, Customer.Id),
};
_requests.Requests.Add(new CreateRequest { Target = quote });
var quoteDetail = new QuoteDetail
{
QuoteId = new EntityReference(Quote.EntityLogicalName, quote.Id),
ProductId = new EntityReference(Product.EntityLogicalName, product.Id),
IsPriceOverridden = true,
PricePerUnit = new Money(20),
Quantity = Convert.ToDecimal(5),
};
_requests.Requests.Add(new CreateRequest { Target = quoteDetail });
My problem is the quote.Id. 我的问题是quote.Id。 I know it is empty until the server processes the request and creates the quote.
我知道它是空的,直到服务器处理请求并创建报价。
Is there a way to tell the server it should use the quotes new id for the quotedetail? 有没有办法告诉服务器它应该使用quotes new id作为quotedetail?
Or is my only way to create the quote itself and then create all details? 还是我创建报价单然后创建所有详细信息的唯一方法?
What can I do to increase performance if I have to do it this way? 如果必须这样做,该怎么做才能提高性能?
Instead of sending CreateRequests explicity, you could change your code to use the OrganizationServiceContext , which locally tracks changes to objects before submitting them to CRM. 您可以更改代码以使用OrganizationServiceContext ,而不是显式发送CreateRequests,它可以在将对象的更改提交到CRM之前本地跟踪对象的更改。
When using the OrganizationServiceContext, you can use AddRelatedObject to both add an object and link it to another one: 使用OrganizationServiceContext时,可以使用AddRelatedObject添加一个对象并将其链接到另一个对象:
Adds a related entity to the OrganizationServiceContext and creates the link that defines the relationship between the two entities in a single request.
将一个相关实体添加到OrganizationServiceContext并创建一个链接,该链接定义单个请求中两个实体之间的关系。
Alternatively you could manually call AddObject and AddLink . 或者,您可以手动调用AddObject和AddLink 。
You final code would look similar to the following: 您的最终代码将类似于以下内容:
using (var context = new OrganizationServiceContext(_serviceProxy))
{
var quote = new Quote
{
QuoteNumber = "123",
Name = "test123",
PriceLevelId = new EntityReference(PriceLevel.EntityLogicalName, pricelevel.Id),
CustomerId = new EntityReference(Account.EntityLogicalName, customer.Id),
};
context.AddObject(quote);
var quoteDetail = new QuoteDetail
{
ProductId = new EntityReference(Product.EntityLogicalName, product.Id),
IsPriceOverridden = true,
PricePerUnit = new Money(20),
Quantity = Convert.ToDecimal(5),
};
context.AddRelatedObject(quote, new Relationship("quote_details"), quoteDetail);
context.SaveChanges();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.