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 8588339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:46:48+00:00 2026-06-11T22:46:48+00:00

I’m not sure if this is possible but I am inserting a lot of

  • 0

I’m not sure if this is possible but I am inserting a lot of entities into the database, and for each item it has say a language property which is an entity. Currently I create a new language object for each item that is being inserted but this is just for testing as it creates a lot of duplicates. The languages should be unique in the database and should just point to an existing language record, the only way I can think of is to do seperate service calls for every insert to lookup a language code and get the id of the existing language which is a lot of overhead.

Is there a way for it to automatically lookup a record based on the language code and use that record?

Thanks

  • 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-06-11T22:46:49+00:00Added an answer on June 11, 2026 at 10:46 pm

    Both ObjectContext and DbContext have methods that try to find an already loaded entity from the context in memory – GetObjectByKey for ObjectContext and Find for DbContext. Using these methods you avoid to load a Language entity more than once from the database.

    Both methods need a primary key value as input parameter to lookup the entity by key. Essentially think of these methods as accessing an internal dictionary that relates the primary key to the entity object.

    Now, refering to your comment…

    Essentially trying to match on a custom value rather than the
    id/primary key value.

    …you can’t use these methods because they rely on knowing the primary key value you want to lookup.

    What I would do is to mimic the logic of the mentioned internal dictionary by your own dictionary that relates your “custom value” to the entity instead of the primary key. I assume that the custom value is unique in the Language table.

    So, the idea would be:

    Given you have entities like this…

    public class MyEntity
    {
        public int Id { get; set; }
        public Language Language { get; set; }
        public string SomeOtherValue { get; set; }
        // ... more
    }
    
    public class Language
    {
        public int Id { get; set; }             // primary key value
        public string CustomValue { get; set; } // the unique custom value
        // ... more
    }
    

    …and you have a collection of data you are using to insert the new entities…

    public class Data
    {
        public string SomeOtherValue { get; set; }
        public string LanguageCustomValue { get; set; }
        // ... more
    }
    
    var dataList = new List<Data>
    {
        new Data { SomeOtherValue = "A", LanguageCustomValue = "EN" },
        new Data { SomeOtherValue = "B", LanguageCustomValue = "FR" },
        new Data { SomeOtherValue = "C", LanguageCustomValue = "EN" },
        new Data { SomeOtherValue = "D", LanguageCustomValue = "EN" },
        // ... more
    }
    

    …then you could use this:

    using (var context = new MyContext())
    {
        var dict = new Dictionary<string, Language>();
        foreach (var data in dataList)
        {
            Language language;
            if (!dict.TryGetValue(data.LanguageCustomValue, out language))
            {
                // load the language only once from the database
                language = context.Languages.SingleOrDefault(l =>
                    l.CustomValue == data.LanguageCustomValue);
                dict.Add(data.LanguageCustomValue, language);
            }
    
            var myEntity = new MyEntity
            {
                SomeOtherValue = data.SomeOtherValue,
                Language = language
            };
    
            context.MyEntities.Add(myEntity);  // or AddObject
        }
        context.SaveChanges();
    }
    

    This would load only one Language entity per LanguageCustomValue, do this only once and doesn’t create duplicates of Language objects in the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with 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.