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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:51:33+00:00 2026-06-15T16:51:33+00:00

For a while I’ve been trying to find a solution as to how I

  • 0

For a while I’ve been trying to find a solution as to how I could use the seed method in the migration configuration without fear of updated data being overwritten next time the seed method is run.

In short my understanding is that the addorupdate, as demonstrated in various tutorials, will reset all values for each object hence overwriting any changes that may have occurred to that object since it was initially seeded.

In my current project I want to be able to seed a set of default emailtemplates. Putting them in the seed method will assure that they are always present in my code. However, I want the user of the application to be able to edit some of the content as they see fit. So I can’t very well have the template being reset everything the seed method runs as it would delete changes.

My solution is this:

    protected override void Seed(Jobboard.Sandbox.Model.JobboardContext context)
    {
        Guid DefaultTemplateGuid = Guid.Parse("xxxxx");

        context.Templates.AddOrUpdate(
              t => t.Guid,
                  context.Templates.FirstOrDefault(x => x.Guid == DefaultTemplateGuid)
                  ?? new Template {
                     Guid = DefaultTemplateGuid,
                     Name = "Default Template",
                     Content = "Some Default Content"
                  }
        );
    }

All in all this seems to work just fine and my question is if anyone can spot any issues doing the seed this way as it’s not exactly the way the EF team suggests using this feature and I would prefer not ending up with a headache over this later on.

Many thanks for reading.

  • 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-15T16:51:35+00:00Added an answer on June 15, 2026 at 4:51 pm

    You can probably save a few trips to the database by not using the AddOrUpdate extension method. Here is a simplified version of your code.

    protected override void Seed(Jobboard.Sandbox.Model.JobboardContext context)
    {
        var defaultTemplateGuid = Guid.Parse("xxxxx");
        var defaultTemplate = context.Templates.SingleOrDefault(
            t => t.Guid == defaultTemplateGuid);
    
        if (defaultTemplate == null)
        {
            context.Templates.Add(
                new Template
                    {
                        Guid = defaultTemplateGuid,
                        Name = "Default Template",
                        Content = "Some Default Content"
                    });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While I could use strings, I would like to understand why this small example
While googling I could find only feistel ciphers and didn't find any relevant information
While searching the interweb for a solution for my VB.net problems I often find
While trying to build the mysql2 gem with ruby 1.9.2-p320 on Fedora 16, I
While trying to install a program from source I found that it installed libraries
While I understand the Math.round/ceil/floor functions in javascript, I have been unable to come
While calling XslCompiledTransform.Transform() method I get this exception : The Writer is closed or
While trying to call an EJB made using NETbeans (using jdk1.7) from a client
While database design I used a field name deleted and use it as Boolean.
While trying to open an excel using ApachePOI I get org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the

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.