Giter Site home page Giter Site logo

abhishekbhalani / entityframework.seeder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dpaquette/entityframework.seeder

1.0 2.0 0.0 718 KB

A collection of helper methods to help seed entity framework databases

License: MIT License

C# 100.00%

entityframework.seeder's Introduction

EntityFramework.Seeder

A collection of helper methods to help seed entity framework databases. When seeding your database with lookup entities, we often currently do the following in our Seed methods:

context.Countries.AddOrUpdate(c => c.Code, new Country[]
        {
            new Country{ Code = "CA", Name = "Canada"},
            new Country{ Code = "USA", Name = "United States"}
            //etc...
        });

While this is managable for small amounts of data, our seed methods can become difficult to manage in many real world scenarios.

Using EntiyFramework.Seeder, you can seed your database from CSV files.

First, create a CSV file with column names matching the properties of your entity. In the case of Country, the file would look as follows

Code, Name
CA, Canada
USA, United States
etc...

Next, include the file in project that contains your Seed method and change the Build Action to Embedded Resource.

Finally, call the SeedFromResource extension method:

context.Countries.SeedFromResource("MyProject.SeedData.countries.csv", c => c.Code)

You also have the option to seed from a csv file on disk using SeedFromFile, or from any stream using SeedFromStream.

##Connecting Related Entities Loading data from a CSV file can be a little more complicated when there are relationships between the entities were are loading. Take Countries and Provinces/States as an example. A Province/State always belongs to a single Country. In this case, create a CSV file that includes the Country Code of the Country that the Province State belongs to:

CountryCode,Code,Name
CA,SK,Saskatchewan
CA,AB,Alberta
US,AZ,Arizona
US,AR,Arkansas
US,CA,California

Now, specify a custom mapping action when seeding from the provinces csv:

context.Countries.SeedFromResource("MyProject.SeedData.countries.csv", c => c.Code);
context.SaveChanges();
context.ProvinceStates.SeedFromResource("MyProject.SeedData.provincestates.csv", p => p.Code,
        new CsvColumnMapping<ProvinceState>("CountryCode", (state, countryCode) =>
            {
                state.Country = context.Countries.Single(c => c.Code == countryCode);
            })
         );

##CsvHelper Configuration Under the covers, EntityFramework.Helper uses CsvHelper to read the CSV files. CsvHelper Configuration can be changed by setting properties on the Seeder.Configuration property. For example, to change the delimiter to a | character:

Seeder.Configuration.Delimiter = "|";

All configuration options available in CsvHelper Configuration can be set here. Configuration changes are global and will apply to any Seed methods that are called after a configuration option is changed. If you need to reset to the default configuration, call the Seeder.ResetConfiguration() method.

##Available on Nuget PM> Install-Package EntityFramework.Seeder.EF6

entityframework.seeder's People

Contributors

dpaquette avatar

Stargazers

Abhishek B. avatar

Watchers

James Cloos avatar Abhishek B. avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.