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 try to add an addons system to my Windows.Net application using Reflection; but
I try get the mp3 flash player to work with my javascript on all
I used to work with VWD and now I try to install the new
Every time I try to do something seemingly-simple in CSS, it doesn't work. I
We try to convert from string to Byte[] using the following Java code: String
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
try { ... } catch (SQLException sqle) { String theError = (sqle).getSQLState(); ... }
I try to fetch a Wikipedia article with Python's urllib: f = urllib.urlopen(http://en.wikipedia.org/w/index.php?title=Albert_Einstein&printable=yes) s
I try to define a schema for XML documents I receive. The documents look
I try to write KSH script for processing a file consisting of name-value pairs,

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.