public static ITrigger Attach(this ITrigger trigger, IAction actuatorAction) { if (trigger == null) throw new ArgumentNullException(nameof(trigger)); if (actuatorAction == null) throw new ArgumentNullException(nameof(actuatorAction)); trigger.Attach(actuatorAction.Execute); return trigger; }
public static void InsertOnSubmit(this DbSet<Newsletter> source, Newsletter newsletter) { if (newsletter.NewsletterID == default(int)) { // New entity source.Add(newsletter); } else { // Existing entity source.Attach(newsletter); } }
public static void InitializePersonalProjects(this ITashaPerson person) { //Work | School | IndividualOther | IndividualMarket SchedulerPersonData data; person.Attach( "SData", data = new SchedulerPersonData() ); ProjectSchedule workSchedule = new ProjectSchedule( person.Household ); ProjectSchedule schoolSchedule = new ProjectSchedule( person.Household ); ProjectSchedule otherSchedule = new ProjectSchedule( person.Household ); ProjectSchedule marketSchedule = new ProjectSchedule( person.Household ); // We could just call the other methods, but this will run much faster data.WorkSchedule = new PersonalProject( workSchedule, person ); data.SchoolSchedule = new PersonalProject( schoolSchedule, person ); data.OtherSchedule = new PersonalProject( otherSchedule, person ); data.MarketSchedule = new PersonalProject( marketSchedule, person ); data.Schedule = new PersonSchedule( person ); }
/// <summary> /// Helper provisorio para realizar um update no entity sem precisar de ir ao banco de dados. /// </summary> /// <param name="context">Contexto do Entity.</param> /// <param name="entitySetName">Nome do EntitySet.</param> /// <param name="entity">Entity Object.</param> public static void Update(this ObjectContext context, string entitySetName, EntityObject entity) { if (entity.EntityKey == null) entity.EntityKey = context.CreateEntityKey(entitySetName, entity); if (entity.EntityState == EntityState.Detached) context.Attach(entity); var stateEntry = context.ObjectStateManager.GetObjectStateEntry(entity.EntityKey); var propertyNameList = stateEntry.CurrentValues.DataRecordInfo.FieldMetadata.Select(pn => pn.FieldType.Name); foreach (var propName in propertyNameList) { stateEntry.SetModifiedProperty(propName); } //var relatedEntities = context.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added).Where( }
public static void AttachAsModified(this ObjectContext context, EntityObject current, EntityObject original) { if (context == null) { throw new ArgumentNullException("context"); } if (current == null) { throw new ArgumentNullException("current"); } if (original == null) { throw new ArgumentNullException("original"); } if (current.EntityState != EntityState.Detached) { context.Detach(current); } Type entityType = current.GetType(); EntityType type = EntityHelper.GetEntityType(context, entityType); PropertyInfo[] source = type.Properties.Select<EdmProperty, PropertyInfo>(delegate(EdmProperty p) { return entityType.GetProperty(p.Name); }).Where<PropertyInfo>(delegate(PropertyInfo p) { return (p != null); }).ToArray<PropertyInfo>(); PropertyDescriptor[] descriptorArray = type.NavigationProperties.Select<NavigationProperty, PropertyDescriptor>(delegate(NavigationProperty p) { return TypeDescriptor.GetProperties(entityType)[p.Name]; }).Where<PropertyDescriptor>(delegate(PropertyDescriptor p) { return !typeof(IEnumerable).IsAssignableFrom(p.PropertyType); }).ToArray<PropertyDescriptor>(); PropertyDescriptor[] descriptorArray2 = descriptorArray.Select<PropertyDescriptor, PropertyDescriptor>(delegate(PropertyDescriptor p) { return EntityHelper.GetReferenceProperty(p); }).ToArray<PropertyDescriptor>(); object[] objArray = source.Select<PropertyInfo, object>(delegate(PropertyInfo p) { return p.GetValue(current, null); }).ToArray<object>(); EntityKey[] keyArray = descriptorArray2.Select<PropertyDescriptor, EntityReference>(delegate(PropertyDescriptor p) { return (EntityReference)p.GetValue(current); }).Select<EntityReference, EntityKey>(delegate(EntityReference p) { if (p == null) { return null; } return p.EntityKey; }).ToArray<EntityKey>(); for (int i = 0; i < source.Length; i++) { source[i].SetValue(current, source[i].GetValue(original, null), null); } for (int j = 0; j < descriptorArray2.Length; j++) { EntityReference reference = (EntityReference)descriptorArray2[j].GetValue(current); EntityReference reference2 = (EntityReference)descriptorArray2[j].GetValue(original); EntityKey entityKey = reference2.EntityKey; EntityObject obj2 = (EntityObject)descriptorArray[j].GetValue(current); if ((obj2 != null) && (obj2.EntityKey != entityKey)) { descriptorArray[j].SetValue(current, null); } reference.EntityKey = entityKey; } context.Attach(current); for (int k = 0; k < source.Length; k++) { PropertyInfo info2 = source[k]; object objA = objArray[k]; object objB = info2.GetValue(original, null); if (!object.Equals(objA, objB)) { info2.SetValue(current, objA, null); } } for (int m = 0; m < descriptorArray2.Length; m++) { EntityReference reference3 = (EntityReference)descriptorArray2[m].GetValue(current); EntityKey key2 = keyArray[m]; if (!object.Equals(reference3.EntityKey, key2)) { reference3.EntityKey = key2; } } }
public static void InsertOnSubmit(this DbSet<Poll> source, Poll poll) { if (poll.PollID == default(int)) { // New entity source.Add(poll); } else { // Existing entity source.Attach(poll); } }
public static void InsertOnSubmit(this DbSet<ShippingMethod> source, ShippingMethod shippingMethod) { if (shippingMethod.ShippingMethodID == default(int)) { // New entity source.Add(shippingMethod); } else { // Existing entity source.Attach(shippingMethod); } }
public static void InsertOnSubmit(this DbSet<Product> source, Product product) { if (product.ProductID == default(int)) { // New entity source.Add(product); } else { // Existing entity source.Attach(product); } }
public static void InsertOnSubmit(this DbSet<Order> source, Order order) { if (order.OrderID == default(int)) { // New entity source.Add(order); } else { // Existing entity source.Attach(order); } }
/// <summary> /// Calculates and stores the best trip chain for /// each type of vehicle (and NPV) /// </summary> /// <param name="chain">The chain to calculate</param> public static void SelectBestPerVehicleType(this ITripChain chain) { ModeSet[] best = chain["BestForVehicle"] as ModeSet[]; var sets = ModeSet.GetModeSets( chain ); if ( best == null ) { best = new ModeSet[TashaRuntime.VehicleTypes.Count + 1]; chain.Attach( "BestForVehicle", best ); } for ( int i = 0; i < best.Length; i++ ) { best[i] = null; } foreach ( var set in sets ) { IVehicleType type = null; foreach ( var mode in set.ChosenMode ) { if ( mode.RequiresVehicle != null ) { type = mode.RequiresVehicle; break; } } int index = TashaRuntime.VehicleTypes.IndexOf( type ); best[index + 1] = ( best[index + 1] == null || best[index + 1].U < set.U ) ? set : best[index + 1]; } }
internal static void CreateHouseholdProjects(this ITashaHousehold household) { SchedHouseholdData data; household.Attach("SData", data = new SchedHouseholdData()); ProjectSchedule jointOtherSchedule = new ProjectSchedule(household); ProjectSchedule jointMarketSchedule = new ProjectSchedule(household); Project jointOtherProject = new HouseholdProject(household, jointOtherSchedule); Project jointMarketProject = new HouseholdProject(household, jointMarketSchedule); data.JointOtherProject = jointOtherProject; data.JointMarketProject = jointMarketProject; }
public static void InsertOnSubmit(this DbSet<Comment> source, Comment comment) { if (comment.CommentID == default(int)) { // New entity source.Add(comment); } else { // Existing entity source.Attach(comment); } }
public static void InsertOnSubmit(this DbSet<Category> source, Category category) { if (category.CategoryID == default(int)) { // New entity source.Add(category); } else { // Existing entity source.Attach(category); } }
public static void InsertOnSubmit(this DbSet<Article> source, Article article) { if (article.ArticleID == default(int)) { // New entity source.Add(article); } else { // Existing entity source.Attach(article); } }
public static void InsertOnSubmit(this DbSet<Forum> source, Forum forum) { if (forum.ForumID == default(int)) { // New entity source.Add(forum); } else { // Existing entity source.Attach(forum); } }
/// <summary> /// <para>Collection of attachment types, which are allowed in comment posts.</para> /// </summary> /// <param name="widget">Widget to call method on.</param> /// <param name="types">Allowed types of post attachments.</param> /// <returns>Reference to provided <paramref name="widget"/>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="widget"/> is a <c>null</c> reference.</exception> /// <seealso cref="IVkontakteCommentsWidget.Attach(string[])"/> public static IVkontakteCommentsWidget Attach(this IVkontakteCommentsWidget widget, params VkontakteCommentsAttach[] types) { Assertion.NotNull(widget); return widget.Attach(types.Select(item => item == VkontakteCommentsAttach.All ? "*" : item.ToString().ToLowerInvariant()).ToArray()); }
internal static PlanBalance SubscribeUserTo(this SongSearchContext ctx, User user, PricingPlan pricingPlan) { if (user.EntityState == System.Data.EntityState.Detached) { //ctx.Detach(user); ctx.Attach(user); } var oldSubs = user.Subscriptions; foreach (var sub in oldSubs) { if (sub.SubscriptionEndDate == null) { sub.SubscriptionEndDate = DateTime.Now; } } //Start a new Subscription var subscription = new Subscription() { SubscriptionStartDate = DateTime.Now, SubscriptionEndDate = null, PricingPlanId = pricingPlan.PricingPlanId, PlanCharge = pricingPlan.PlanCharge.GetValueOrDefault() }; user.Subscriptions.Add(subscription); ctx.Subscriptions.AddObject(subscription); // Adjust current plan user.PricingPlanId = pricingPlan.PricingPlanId; // if user was already on a plan, switch the balance over; if not, open a new balance PlanBalance balance; if (user.IsPlanOwner) { balance = user.PlanBalance; balance.PricingPlanId = pricingPlan.PricingPlanId; balance.LastUpdatedByUserId = user.UserId; balance.LastUpdatedOn = DateTime.Now; } else { balance = new PlanBalance() { PricingPlanId = pricingPlan.PricingPlanId, NumberOfCatalogAdmins = 1, NumberOfInvitedUsers = 1, NumberOfSongs = 0, LastUpdatedByUserId = user.UserId, LastUpdatedOn = DateTime.Now }; user.PlanUserId = user.UserId; balance.Users.Add(user); ctx.PlanBalances.AddObject(balance); } return balance; }