本文整理汇总了C#中ObjectContext.AddObject方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectContext.AddObject方法的具体用法?C# ObjectContext.AddObject怎么用?C# ObjectContext.AddObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectContext
的用法示例。
在下文中一共展示了ObjectContext.AddObject方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddExternalIdMapping
public static void AddExternalIdMapping(ObjectContext context, EventOccurrenceV2 source, EventOccurrence target)
{
if (!string.IsNullOrEmpty(source.OccurrenceExternalId))
{
var mappingExists = context.CreateObjectSet<DataImportEntityIdMap>()
.Any(m => m.EntityName == "EventOccurrence" &&
m.InternalId == target.Id &&
m.ExternalId == source.OccurrenceExternalId);
if (!mappingExists)
{
context.AddObject("DataImportEntityIdMaps", new DataImportEntityIdMap
{
EntityName = "EventOccurrence",
DataImportId = 2,
InternalId = target.Id,
ExternalId = source.OccurrenceExternalId
});
context.SaveChanges();
}
}
}
示例2: AddExternalIdMapping
public static void AddExternalIdMapping(ObjectContext context, EventV2 source, Event theEvent)
{
if (!string.IsNullOrEmpty(source.EventExternalId))
{
var existingMappings = context.CreateObjectSet<DataImportEntityIdMap>()
.Where(m => m.EntityName == "Event" && m.InternalId == theEvent.Id)
.ToList();
if (existingMappings.Count == 1)
{
if (existingMappings[0].ExternalId != source.EventExternalId)
{
// Update ExternalId on existing mapping
existingMappings[0].ExternalId = source.EventExternalId;
}
}
else
{
// Remove ambiguous mappings (if any)
if (existingMappings.Count > 1)
{
foreach (var mapping in existingMappings)
{
context.DeleteObject(mapping);
}
}
// Create new mapping
context.AddObject("DataImportEntityIdMaps", new DataImportEntityIdMap
{
EntityName = "Event",
DataImportId = 2,
InternalId = theEvent.Id,
ExternalId = source.EventExternalId
});
}
}
}
示例3: CreateEventDiscountCode
private static DiscountCode CreateEventDiscountCode(ObjectContext context, DiscountCodeDtoV2 code)
{
int discountTypeId = LookupDiscountType(context, code.DiscountType);
var discountCode = new DiscountCode
{
Code = code.Code,
Description = code.Description,
DiscountTypeId = discountTypeId,
DiscountValue = Convert.ToDecimal(code.DiscountValue, CultureInfo.InvariantCulture),
StartDate = string.IsNullOrEmpty(code.StartDate) ? new DateTime?() : DateTime.Parse(code.StartDate, CultureInfo.InvariantCulture),
EndDate = string.IsNullOrEmpty(code.StartDate) ? new DateTime?() : DateTime.Parse(code.EndDate, CultureInfo.InvariantCulture),
IsEnabled = bool.Parse(code.IsEnabled),
IsGroupCode = bool.Parse(code.IsGroupCode),
IsFromOrgUnit = false,
RequiredGroupSize = int.Parse(code.RequiredGroupSize, CultureInfo.InvariantCulture),
};
context.AddObject("DiscountCodes", discountCode);
return discountCode;
}
示例4: ApplyChangesToEntity
/// <summary>
/// Apply changes from the newEntity to the original entity
/// </summary>
/// <param name="objectContext">ObjectContext instance.</param>
/// <param name="originalTrackedEntity">original entity which is tracked by the context.</param>
/// <param name="newEntity">newEntity which contains all the changed values.</param>
private static void ApplyChangesToEntity(ObjectContext objectContext, object originalTrackedEntity, object newEntity)
{
ObjectStateEntry objectStateEntry = objectContext.ObjectStateManager.GetObjectStateEntry(originalTrackedEntity);
string entitySetName = ObjectContextServiceProvider.GetEntitySetName(objectStateEntry, objectContext.DefaultContainerName);
if (objectStateEntry.State == EntityState.Added)
{
// In the case of batching if we do a PUT after POST in the same changeset, we need to detach and re-add.
objectContext.Detach(originalTrackedEntity);
objectContext.AddObject(entitySetName, newEntity);
}
else
{
#pragma warning disable 618
// Since we use to do this in V1, keeping the same code there.
// Apply property changes as specified in the new object.
objectContext.ApplyPropertyChanges(entitySetName, newEntity);
#pragma warning restore 618
}
}
示例5: BuildNewOccurrence
public static EventOccurrence BuildNewOccurrence(ObjectContext objectContext, int newEventId, EventOccurrence item)
{
var newOccurrence = new EventOccurrence
{
ContactName = item.ContactName,
ContactEmail = item.ContactEmail,
ContactPhone = item.ContactPhone,
ContactPhoneExtension = item.ContactPhoneExtension,
Cost = item.Cost,
CostCenter = item.CostCenter,
OrgUnitId = item.OrgUnitId,
MaximumAttendees = item.MaximumAttendees,
LastUpdated = System.DateTime.UtcNow,
PaymentProcessorConfigurationId = item.PaymentProcessorConfigurationId,
IsRegistrationEnabled = item.IsRegistrationEnabled,
IsNotificationListEnabled = item.IsNotificationListEnabled,
IsNotifyContactEnabled = item.IsNotifyContactEnabled,
SpecialInstructions = item.SpecialInstructions,
LocationOrgUnitId = item.LocationOrgUnitId,
LocationName = item.LocationName,
Address1 = item.Address1,
Address2 = item.Address2,
City = item.City,
PostalCode = item.PostalCode,
StateId = item.StateId,
CountryId = item.CountryId,
Latitude = item.Latitude,
Longitude = item.Longitude,
IsGuestDemographicInfoRequired = item.IsGuestDemographicInfoRequired
};
newOccurrence.SetPresenter(item.Presenter);
newOccurrence.SetIsEnabled(item.IsEnabled);
newOccurrence.SetEventId(newEventId);
newOccurrence.SetAddress1(item.Address1);
newOccurrence.SetAddress2(item.Address2);
newOccurrence.SetRegistrationDates(item.RegistrationStartDate, item.RegistrationEndDate);
if (item.IsPriceScheduleEnabled)
newOccurrence.EnablePricingSchedule();
else
newOccurrence.DisablePricingSchedule();
CaptureCostScheduleData(item, newOccurrence);
objectContext.AddObject("EventOccurrences", newOccurrence);
objectContext.SaveChanges();
//event occurrence documents
foreach (var doc in item.EventOccurrencesDocuments)
{
var newDoc = new EventOccurrencesDocument()
{
DocumentPath = doc.DocumentPath,
EventOccurrence = newOccurrence
};
newOccurrence.EventOccurrencesDocuments.Add(newDoc);
}
//event occurrence dates
foreach (var dateItem in item.EventOccurrenceDates)
{
newOccurrence.AddOccurrenceDate(dateItem.StartDate, dateItem.EndDate);
}
objectContext.SaveChanges();
return newOccurrence;
}