DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Strategies for Improving the Performance of Applications Using EF Core
  • Data Privacy and Security: A Developer's Guide to Handling Sensitive Data With DuckDB
  • What Is SQL Injection and How Can It Be Avoided?
  • Snowflake Data Time Travel

Trending

  • How to Use AWS Aurora Database for a Retail Point of Sale (POS) Transaction System
  • Infrastructure as Code (IaC) Beyond the Basics
  • How GitHub Copilot Helps You Write More Secure Code
  • After 9 Years, Microsoft Fulfills This Windows Feature Request
  1. DZone
  2. Data Engineering
  3. Databases
  4. Quick Tip: Seeding Large Data in Entity Framework Core 2.1

Quick Tip: Seeding Large Data in Entity Framework Core 2.1

Populating small tables isn't a problem with Entity Framework Core, but what about large amounts of data? Today, we show how to feed large data into your entities.

By 
Jonathan Danylko user avatar
Jonathan Danylko
·
Nov. 20, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
17.4K Views

Join the DZone community and get the full member experience.

Join For Free

Seeding has always been a problem when it comes to Entity Framework.

Where do you place your seeding code? In your Startup.cs? In a helper class?

Well, the Entity Framework team has done it again. The Entity<T> class has a method called HasData(). This HasData method allows you to pass in either a single entity of T or a list of T entities to automatically populate your table.

Julie Lerman also has an absolutely awesome deep dive on HasData Seeding for Entity Framework. Definitely a must read!

For small lookup tables, this is great, but what if you have a LOT of data required for the application to function properly?

Use the [Re]source, Luke!

I remember using Resource files a long time ago. How hard it was to create one, build one, and maintain it. It was excruciatingly painful.

Maybe it was my lack of using them or the tools became better, but I didn't like them at all.

Now they're cool again.

They're now easier to work with and provide a huge benefit for storing default data in a web app.

Perfect for Internationalization and, now, seed data.

Let's create one.

  1. Go to your ASP.NET Core (or WebForms or MVC app) project.
  2. Add a new Resource.resx file and call it whatever you want (for example, MyAppResources.resx).
  3. The first column (Name) is the name you'll refer to in your web app when initially grabbing data (for example, call it StateXml).
  4. In the second column, this is where you place the contents of your seed data (Value column).
  5. Finally, at the top of the grid near the middle, there is an Access Modifier. Change it to Public. This will allow you to access the resource.

With SQL Server 2014 and higher, you can go to SQL Server Management Studio (SSMS) and type either:

SELECT * FROM States FOR XML AUTO

or

SELECT * FROM States FOR JSON AUTO

This will give you an XML or JSON version of your data for your Resource file.

Once you have your resource file saved, we can add the following three lines in our DbContext (or wherever it makes sense for your application). The best place to put this is in your OnModelCreating in your DbContext for your Entities because of the modelBuilder reference.

(The example below is based on the listed scenario from above).
// State Items
var jsonStateList = MyAppResources.StateXml;
var states = JsonConvert.DeserializeObject<List<State>>(jsonStateList);
modelBuilder.Entity<State>().HasData(states);

I know I'm using JSON for my example, but you can easily use XML instead.

If you want to learn more about serializing/deserializing objects using XML, check out my Serialization/Deserialization Series.

Ahem...What About a Script?

I know what you're thinking...why not place a SQL Script inside the resource file instead?

Two reasons:

  1. A script has too much cruft. While I admit it would be easier with a script generated from SQL Server, but it would be bloated with a lot of 'INSERT INTO' statements. All we want in our resource file is pure, serialized data.
  2. We want to keep our entity tables granular. If a couple of tables were not populated, I don't want to run an entire script for a couple of empty tables.

Also, we could just run a SQL Script, but, lately, Entity Framework has been becoming more code-centric with its code-first approach, where even the database changes are reflected in code per EF Migrations.

*Bonus Benefit*

Did you know if you have this data in a Resource file, you have a ready-to-go In-Memory database for your unit tests?

Start up your unit tests and prepare the DbContext to include all of your tables and you can run everything in-memory.

It's absolutely one of the fastest ways to mock a database.

Conclusion

This quick tip shows you how to take a large amount of data and prepopulate your tables at runtime. SQL Scripts are helpful, but versioning the code (or data in this case) is more important.

How do you seed large amounts of data in your application? SQL Script? Code? cUrl? Post your comments below and let's discuss this approach!
Database Entity Framework Data (computing) Framework sql

Published at DZone with permission of Jonathan Danylko, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Strategies for Improving the Performance of Applications Using EF Core
  • Data Privacy and Security: A Developer's Guide to Handling Sensitive Data With DuckDB
  • What Is SQL Injection and How Can It Be Avoided?
  • Snowflake Data Time Travel

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!