Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3321202
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:02:04+00:00 2026-05-17T23:02:04+00:00

I try to work with link to entity, and i want to work directly

  • 0

I try to work with link to entity, and i want to work directly with my entity in my application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Calandar.Business.Manager.Data;

namespace Calandar.Business.Models.Args
{
    public class SaveExpertArgs
    {
        public ExpertEntity Expert { get; set; }
        public SaveExpertArgs(ExpertEntity expert)
        {
            Expert = expert;
        }
    }
}

public ExpertEntity SaveExpert(SaveExpertArgs args)
{            
    string connString = ConfigurationManager.ConnectionStrings["CalendarContainer"].ConnectionString;

    using (CalendarContainer dbContext = new CalendarContainer(connString))
    {             
        ExpertEntity expert = (from e in dbContext.ExpertEntities
                               where e.ExpertIdentifier == args.Expert.ExpertIdentifier
                               select e).FirstOrDefault();
        if (expert == null)
        {
            args.Expert.ExpertIdentifier = Guid.NewGuid();
            dbContext.AddToExpertEntities(args.Expert);
        }
        else
        {
            dbContext.ExpertEntities.ApplyCurrentValues(args.Expert);               

            foreach (TimeSlotEntity t in args.Expert.TimeSlotEntities)
            {
                dbContext.TimeSlotEntities.ApplyCurrentValues(t);
            }
        }              

        dbContext.SaveChanges();
        return args.Expert;
    }
}

I try to save my expert entity and it’s working, but i dont know how to save my EntityCollection in my expert Entity. some body can help me ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:02:04+00:00Added an answer on May 17, 2026 at 11:02 pm

    Ok i found how i could update my entity collection.

    There is my technique. I did not find any documentation on the technique, so give me your feed back

    public ExpertEntity SaveExpert(SaveExpertArgs args)
            {
    
                string connString = ConfigurationManager.ConnectionStrings["CalendarContainer"].ConnectionString;
                using (CalendarContainer dbContext = new CalendarContainer(connString))
                {
                    ExpertEntity expert = (from e in dbContext.ExpertEntities
                                           where e.ExpertIdentifier == args.Expert.ExpertIdentifier
                                           select e).FirstOrDefault();
                    if (expert == null)
                    {
                        args.Expert.ExpertIdentifier = Guid.NewGuid();
                        dbContext.AddToExpertEntities(args.Expert);
                    }
                    else
                    {
                        dbContext.ExpertEntities.ApplyCurrentValues(args.Expert);
                        GenericUpdateEntityCollection(args.Expert.TimeSlotEntities, dbContext);
                    }
                    dbContext.SaveChanges();
                    return args.Expert;
                }
            } 
    
    
    private void GenericUpdateEntityCollection<T>(EntityCollection<T> collection, ObjectContext dbContext)
                where T : EntityObject, new()
            {
                int count = collection.Count();
                int current = 0;
                List<T> collectionItemList = collection.ToList();
                bool isAdded = false;
                while (current < count)
                {
                    Object obj = null;
                    // Essai de récupéré l'objet dans le context pour le mettre à jour
                    dbContext.TryGetObjectByKey(collectionItemList[current].EntityKey, out obj);
                    if (obj == null)
                    {
                        // Si l'objet n'existe pas, on en créer un nouveau
                        obj = new TimeSlotEntity();
                        // On lui donne l'entity Key du nouvelle objet
                        ((T)obj).EntityKey = collectionItemList[current].EntityKey;
                        // On l'ajoute au context, dans le timeslot
                        dbContext.AddObject(((T)obj).EntityKey.EntitySetName, obj);
                        // On récupère l'objet du context qui à le même entity key que le nouveau recu en pramètre, le but est d'avoir un context d'attacher
                        dbContext.TryGetObjectByKey(collectionItemList[current].EntityKey, out obj);
                        // On change l'état de l'objet dans le context pour modifier, car 
                        dbContext.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
                        // On change l'état de l'objet passé en paramètre pour qu'il soit au même state que celui dans le context
                        collection.CreateSourceQuery().Context.ObjectStateManager.ChangeObjectState(collectionItemList[current], System.Data.EntityState.Modified);
                        // On place notre flag pour dire que nous avons ajouter dans le context les donnée
                        isAdded = true;
                    }
    
                    if (obj != null)
                    {
                        // On applique les changements de l'objet passé en parametre à celui dans le context
                        dbContext.ApplyCurrentValues<T>(((T)obj).EntityKey.EntitySetName,collectionItemList[current]);
                        // On replace les state des deux objet, celui dans le context et celui passé en parametre à added pour la sauvegarde.
                        if (isAdded)
                        {
                            dbContext.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Added);
                            collection.CreateSourceQuery().Context.ObjectStateManager.ChangeObjectState(collectionItemList[current], System.Data.EntityState.Added);
                        }
                    }
                    current++;
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a newbie in Python. I try to work with server using Thrift
i try to use jquery .text() but not really work : Before <a id=group_name
I want to learn and work with initialize.php so I try to build simple
I just work on web-based application for iPhone, And I try to retrieve data
I work on Intel-Atom 32bits (Assembly AT&T). I want to link my ISR to
I try to post my image link to Facebook fan page using Facebook C#
I have a string like this. <p class='link'>try</p>bla bla</p> I want to get only
I try to use this code for toggling text on the link: var showText=<span>Open</span>
In my application users enter a url and I try to open the link
I try to work with ctags and it simply doesn't work. I follow this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.