/// <summary> /// Fakes the Create message /// </summary> /// <param name="context"></param> /// <param name="fakedService"></param> protected static void FakeCreate(XrmFakedContext context, IOrganizationService fakedService) { A.CallTo(() => fakedService.Create(A<Entity>._)) .ReturnsLazily((Entity e) => { if (e == null) { throw new InvalidOperationException("The entity must not be null"); } if (string.IsNullOrWhiteSpace(e.LogicalName)) { throw new InvalidOperationException("The LogicalName property must not be empty"); } if (e.Id != Guid.Empty && context.Data.ContainsKey(e.LogicalName) && context.Data[e.LogicalName].ContainsKey(e.Id)) { throw new InvalidOperationException(string.Format("There is already a record of entity {0} with id {1}, can't create with this Id.", e.LogicalName, e.Id)); } //Add entity to the context if (e.Id == Guid.Empty) { e.Id = Guid.NewGuid(); //Hack for Dynamic Entities where the Id property doesn't populate the "entitynameid" primary key if (!e.GetType().IsSubclassOf(typeof(Entity))) { var primaryKeyAttribute = string.Format("{0}id", e.LogicalName); e[primaryKeyAttribute] = e.Id; } } context.AddEntity(e); return e.Id; }); }