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

  • Home
  • SEARCH
  • 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 8473907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:29:00+00:00 2026-06-10T17:29:00+00:00

We have custom code that wipes and initializes our test database by creating a

  • 0

We have custom code that wipes and initializes our test database by creating a few objects and saving them to the DatabaseContext (we use Migrations for the schema, but not for the data). However, our tester has created a lot more data, and doesn’t want to lose her test data when we do a clean deploy. I therefore have to figure out how to get the data from SQL Server into C# (once off), so it an be run with our data-initialization program.

My initial approach was to manually copy-paste the data into C# object initializers, but this got old quickly, since there are 100+ rows to copy.

My second approach was to create the objects using a SELECT statement in SQL Server, mixing the C# initializer syntax into the SQL SELECT statement, and then copying the C# strings generated by SQL Server back into Visual Studio.

Although the second approach seems OK, I was wondering if there’s an obvious solution (apart from simply running a SQL INSERT script every time) that I am missing.

  • 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-10T17:29:01+00:00Added an answer on June 10, 2026 at 5:29 pm

    I ended up going with the approach mentioned in the question. Generating C# object initialializers in SQL Select Statements.

    A few things I picked up, mostly obvious:

    • CAST AS VARCHAR for all numeric fields (although SQL Server will give error if you don’t).

    • Dates: convert to VARCHAR and surround with DateTime.Parse()

    • Use a CASE statement to convert SQL bool value (1/0) to true/false as strings.

    • Enums: generate a cast.

    • If using autonumber ids, remember to prefix instance names with a letter(s). Add the object to DataContext after initializing it.

    Don’t be afraid to reference your C# code in the generated code. It isn’t going to run in SQL Server in any case.

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

Sidebar

Related Questions

I have several Entity Framework Code First DbContext objects that use a custom Initializer.
I have the following code that processes an INotification (custom interface) var mgr =
In WPF, I have a custom control that inherits from TreeView. The code is
I have custom code inside of a SSRS report that needs to get the
I have written some custom R code that wraps a third-party binary. One of
I would like to have a custom snippet of html form code that takes
We have implemented some custom tooltip-drawing code that fires on Tick events of a
Ok I have a page that uses 3 plugins and custom code. Fullscreenr ScrollTo
I have a custom user control with a bool Selected variable, and code that
I have one custom website with code created 100% by me. I need to

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.