A couple months ago, a colleague of mine recommended that I try out NBuilder.
NBuilder makes the aforementioned EF Code First pains suck less. Less meaning that the pains still suck, but they are more tolerable. If you’re not familiar with NBuilder, the NBuilder Website describes NBuilder as the following:
“Through a fluent, extensible interface, NBuilder allows you to rapidly create test data, automatically assigning values to properties and public fields that are of type of the built in .NET data types (e.g. ints and strings). NBuilder allows you to override for properties you are interested in using lambda expressions.”
That is exactly what it is. But, how does it help EF Code First dropping tables requirement suck less? Well, you can create an database initializer class that derives from DropCreateDatabaseIfModelChanges<T>, where T is your DbContext class, that contains a Seed method that will insert data into your data tables. More on this later… If you’re lost and/or not familiar with any of these terms (with the exception of NBuilder), I recommend that you check out Steve Sanderson’s Mix 11 cast: Scaffolding – ASP.NET, NuGet, Entity Framework Code First and More.
BTW, NBuilder is available via NuGet.
NBuilder contains facilities to randomly generate all kinds of data and data types including primitive types, phone numbers, date and time, first name, last name, addresses, etc... NBuilder also contains address randomizers; however, the randomizer supporting US addresses is not quite as useful as the randomizer for UK addresses. Therefore, I created a class to generate more useful US addresses. Below is a class snippet for this US address randomizer:
UsAddressRandomizer
using System.Collections.Generic; using FizzWare.NBuilder.Generators; namespace FizzWare.NBuilder { public class UsAddressRandomizer { private static readonly IRandomGenerator Generator = new RandomGenerator(); #region Data Generation private static readonly List<UsAddressFake> States = new List<UsAddressFake> { new UsAddressFake { StateName = "Alaska", StateCode = "AK", ZipRangeBegin = 99500, ZipRangeEnd = 99999}, new UsAddressFake { StateName = "Alabama", StateCode = "AL", ZipRangeBegin = 35000, ZipRangeEnd = 36999}, new UsAddressFake { StateName = "Arkansas", StateCode = "AR", ZipRangeBegin = 71600, ZipRangeEnd = 72999}, new UsAddressFake { StateName = "Arizona", StateCode = "AZ", ZipRangeBegin = 85000, ZipRangeEnd = 86599}, new UsAddressFake { StateName = "California", StateCode = "CA", ZipRangeBegin = 90000, ZipRangeEnd = 96699}, new UsAddressFake { StateName = "Colorado", StateCode = "CO", ZipRangeBegin = 80000, ZipRangeEnd = 81699}, new UsAddressFake { StateName = "Connecticut", StateCode = "CT", ZipRangeBegin = 6000, ZipRangeEnd = 6999}, new UsAddressFake { StateName = "Delaware", StateCode = "DE", ZipRangeBegin = 19700, ZipRangeEnd = 19999}, new UsAddressFake { StateName = "Florida", StateCode = "FL", ZipRangeBegin = 32000, ZipRangeEnd = 34999}, new UsAddressFake { StateName = "Georgia", StateCode = "GA", ZipRangeBegin = 30000, ZipRangeEnd = 31999}, new UsAddressFake { StateName = "Hawaii", StateCode = "HI", ZipRangeBegin = 96700, ZipRangeEnd = 96899}, new UsAddressFake { StateName = "Iowa", StateCode = "IA", ZipRangeBegin = 50000, ZipRangeEnd = 52899}, new UsAddressFake { StateName = "Idaho", StateCode = "ID", ZipRangeBegin = 83200, ZipRangeEnd = 83899}, new UsAddressFake { StateName = "Illinois", StateCode = "IL", ZipRangeBegin = 60000, ZipRangeEnd = 62999}, new UsAddressFake { StateName = "Indiana", StateCode = "IN", ZipRangeBegin = 46000, ZipRangeEnd = 47999}, new UsAddressFake { StateName = "Kansas", StateCode = "KS", ZipRangeBegin = 66000, ZipRangeEnd = 67999}, new UsAddressFake { StateName = "Kentucky", StateCode = "KY", ZipRangeBegin = 40000, ZipRangeEnd = 42799}, new UsAddressFake { StateName = "Louisiana", StateCode = "LA", ZipRangeBegin = 70000, ZipRangeEnd = 71499}, new UsAddressFake { StateName = "Massachusetts", StateCode = "MA", ZipRangeBegin = 1000, ZipRangeEnd = 2799}, new UsAddressFake { StateName = "Maryland", StateCode = "MD", ZipRangeBegin = 20600, ZipRangeEnd = 21999}, new UsAddressFake { StateName = "Maine", StateCode = "ME", ZipRangeBegin = 3900, ZipRangeEnd = 4999}, new UsAddressFake { StateName = "Michigan", StateCode = "MI", ZipRangeBegin = 48000, ZipRangeEnd = 49999}, new UsAddressFake { StateName = "Minnesota", StateCode = "MN", ZipRangeBegin = 55000, ZipRangeEnd = 56799}, new UsAddressFake { StateName = "Missouri", StateCode = "MO", ZipRangeBegin = 63000, ZipRangeEnd = 65899}, new UsAddressFake { StateName = "Mississippi", StateCode = "MS", ZipRangeBegin = 38600, ZipRangeEnd = 39799}, new UsAddressFake { StateName = "Montana", StateCode = "MT", ZipRangeBegin = 59000, ZipRangeEnd = 59999}, new UsAddressFake { StateName = "North Carolina", StateCode = "NC", ZipRangeBegin = 26900, ZipRangeEnd = 28999}, new UsAddressFake { StateName = "North Dakota", StateCode = "ND", ZipRangeBegin = 58000, ZipRangeEnd = 58899}, new UsAddressFake { StateName = "Nebraska", StateCode = "NE", ZipRangeBegin = 68000, ZipRangeEnd = 69399}, new UsAddressFake { StateName = "New Hampshire", StateCode = "NH", ZipRangeBegin = 3000, ZipRangeEnd = 3999}, new UsAddressFake { StateName = "New Jersey", StateCode = "NJ", ZipRangeBegin = 7000, ZipRangeEnd = 8999}, new UsAddressFake { StateName = "New Mexico", StateCode = "NM", ZipRangeBegin = 87000, ZipRangeEnd = 88499}, new UsAddressFake { StateName = "Nevada", StateCode = "NV", ZipRangeBegin = 88900, ZipRangeEnd = 89899}, new UsAddressFake { StateName = "New York", StateCode = "NY", ZipRangeBegin = 500, ZipRangeEnd = 6399}, new UsAddressFake { StateName = "Ohio", StateCode = "OH", ZipRangeBegin = 43000, ZipRangeEnd = 45999}, new UsAddressFake { StateName = "Oklahoma", StateCode = "OK", ZipRangeBegin = 73000, ZipRangeEnd = 74999}, new UsAddressFake { StateName = "Oregon", StateCode = "OR", ZipRangeBegin = 97000, ZipRangeEnd = 97999}, new UsAddressFake { StateName = "Pennsylvania", StateCode = "PA", ZipRangeBegin = 15000, ZipRangeEnd = 19699}, new UsAddressFake { StateName = "Rhode Island", StateCode = "RI", ZipRangeBegin = 2800, ZipRangeEnd = 2999}, new UsAddressFake { StateName = "South Carolina", StateCode = "SC", ZipRangeBegin = 29000, ZipRangeEnd = 29999}, new UsAddressFake { StateName = "South Dakota", StateCode = "SD", ZipRangeBegin = 57000, ZipRangeEnd = 57799}, new UsAddressFake { StateName = "Tennessee", StateCode = "TN", ZipRangeBegin = 37000, ZipRangeEnd = 38599}, new UsAddressFake { StateName = "Texas", StateCode = "TX", ZipRangeBegin = 75000, ZipRangeEnd = 79999}, new UsAddressFake { StateName = "Utah", StateCode = "UT", ZipRangeBegin = 84000, ZipRangeEnd = 84799}, new UsAddressFake { StateName = "Vermont", StateCode = "VT", ZipRangeBegin = 5000, ZipRangeEnd = 5999}, new UsAddressFake { StateName = "Virginia", StateCode = "VA", ZipRangeBegin = 22000, ZipRangeEnd = 24699}, new UsAddressFake { StateName = "Washington", StateCode = "WA", ZipRangeBegin = 98000, ZipRangeEnd = 99499}, new UsAddressFake { StateName = "Wisconsin", StateCode = "WI", ZipRangeBegin = 53000, ZipRangeEnd = 54999}, new UsAddressFake { StateName = "West Virginia", StateCode = "WV", ZipRangeBegin = 24700, ZipRangeEnd = 26899}, new UsAddressFake { StateName = "Wyoming", StateCode = "WY", ZipRangeBegin = 82000, ZipRangeEnd = 83199} }; private static readonly List<string> Cities = new List<string> { "Abington", "Acton", "Acushnet", "Adams", "Agawam", "Alford", "Amesbury", "Amherst", "Andover", "Aquinnah", "Arlington", "Ashburnham", "Ashby", "Ashfield", "Ashland", "Athol", "Attleboro", "Auburn", "Avon", "Ayer", "Barnstable", "Barre", "Becket", "Bedford", "Belchertown", "Bellingham", "Belmont", "Berkley", "Berlin", "Bernardston", "Beverly", "Billerica", "Blackstone", "Blandford", "Bolton", "Boston", "Bourne", "Boxborough", "Boxford", "Boylston", "Braintree", "Brewster", "Bridgewater", "Brimfield", "Brockton", "Brookfield", "Brookline", "Buckland", "Burlington", "Cambridge", "Canton", "Carlisle", "Carver", "Charlemont", "Charlton", "Chatham", "Chelmsford", "Chelsea", "Cheshire", "Chester", "Chesterfield", "Chicopee", "Chilmark", "Clarksburg", "Clinton", "Cohasset", "Colrain", "Concord", "Conway", "Cummington", "Dalton", "Danvers", "Dartmouth", "Dedham", "Deerfield", "Dennis", "Dighton", "Douglas", "Dover", "Dracut", "Dudley", "Dunstable", "Duxbury", "East Bridgewater", "East Brookfield", "East Longmeadow", "Eastham", "Easthampton", "Easton", "Edgartown", "Egremont", "Erving", "Essex", "Everett", "Fairhaven", "Fall River", "Falmouth", "Fitchburg", "Florida", "Foxborough", "Framingham", "Franklin", "Freetown", "Gardner", "Georgetown", "Gill", "Gloucester", "Goshen", "Gosnold", "Grafton", "Granby", "Granville", "Great Barrington", "Greenfield", "Groton", "Groveland", "Hadley", "Halifax", "Hamilton", "Hampden", "Hancock", "Hanover", "Hanson", "Hardwick", "Harvard", "Harwich", "Hatfield", "Haverhill", "Hawley", "Heath", "Hingham", "Hinsdale", "Holbrook", "Holden", "Holland", "Holliston", "Holyoke", "Hopedale", "Hopkinton", "Hubbardston", "Hudson", "Hull", "Huntington", "Ipswich", "Kingston", "Lakeville", "Lancaster", "Lanesborough", "Lawrence", "Lee", "Leicester", "Lenox", "Leominster", "Leverett", "Lexington", "Leyden", "Lincoln", "Littleton", "Longmeadow", "Lowell", "Ludlow", "Lunenburg", "Lynn", "Lynnfield", "Malden", "Manchester-by-the-Sea", "Mansfield", "Marblehead", "Marion", "Marlborough", "Marshfield", "Mashpee", "Mattapoisett", "Maynard", "Medfield", "Medford", "Medway", "Melrose", "Mendon", "Merrimac", "Methuen", "Middleborough", "Middlefield", "Middleton", "Milford", "Millbury", "Millis", "Millville", "Milton", "Monroe", "Monson", "Montague", "Monterey", "Montgomery", "Mount Washington", "Nahant", "Nantucket", "Natick", "Needham", "New Ashford", "New Bedford", "New Braintree", "New Marlborough", "New Salem", "Newbury", "Newburyport", "Newton", "Norfolk", "North Adams", "North Andover", "North Attleborough", "North Brookfield", "North Reading", "Northampton", "Northborough", "Northbridge", "Northfield", "Norton", "Norwell", "Norwood", "Oak Bluffs", "Oakham", "Orange", "Orleans", "Otis", "Oxford", "Palmer", "Paxton", "Peabody", "Pelham", "Pembroke", "Pepperell", "Peru", "Petersham", "Phillipston", "Pittsfield", "Plainfield", "Plainville", "Plymouth", "Plympton", "Princeton", "Provincetown", "Quincy", "Randolph", "Raynham", "Reading", "Rehoboth", "Revere", "Richmond", "Rochester", "Rockland", "Rockport", "Rowe", "Rowley", "Royalston", "Russell", "Rutland", "Salem", "Salisbury", "Sandisfield", "Sandwich", "Saugus", "Savoy", "Scituate", "Seekonk", "Sharon", "Sheffield", "Shelburne", "Sherborn", "Shirley", "Shrewsbury", "Shutesbury", "Somerset", "Somerville", "South Hadley", "Southampton", "Southborough", "Southbridge", "Southwick", "Spencer", "Springfield", "Sterling", "Stockbridge", "Stoneham", "Stoughton", "Stow", "Sturbridge", "Sudbury", "Sunderland", "Sutton", "Swampscott", "Swansea", "Taunton", "Templeton", "Tewksbury", "Tisbury", "Tolland", "Topsfield", "Townsend", "Truro", "Tyngsborough", "Tyringham", "Upton", "Uxbridge", "Wakefield", "Wales", "Walpole", "Waltham", "Ware", "Wareham", "Warren", "Warwick", "Washington", "Watertown", "Wayland", "Webster", "Wellesley", "Wellfleet", "Wendell", "Wenham", "West Boylston", "West Bridgewater", "West Brookfield", "West Newbury", "West Springfield", "West Stockbridge", "West Tisbury", "Westborough", "Westfield", "Westford", "Westhampton", "Westminster", "Weston", "Westport", "Westwood", "Weymouth", "Whately", "Whitman", "Wilbraham", "Williamsburg", "Williamstown", "Wilmington", "Winchendon", "Winchester", "Windsor", "Winthrop", "Woburn", "Worcester", "Worthington", "Wrentham", "Yarmouth" }; private static readonly List<string> Streets = new List<string> { "107th Street", "10th Avenue", "10th Street", "11th Avenue", "11th Street", "12th Avenue", "12th Street", "137th Street", "13th Street", "14th Road", "14th Street", "17th Street", "181st Street", "18th Street", "19th Street", "1st Avenue", "1st Street", "23rd Street", "25th Street", "2nd Avenue", "2nd Street", "30th Avenue", "31st Street", "33rd Street", "34th Avenue", "35th Avenue", "35th Street", "36th Street", "37th Avenue", "39th Avenue", "3rd Avenue", "3rd Street", "45th Avenue", "47th Avenue", "4th Avenue", "4th Street", "51st Avenue", "52 Chambers Street", "52nd Street", "5th Avenue", "5th Street", "68th Street", "6th Avenue", "6th Street", "76th Street", "77th Street", "78th Street", "79th Street", "7th Avenue", "7th Avenue South", "7th Street", "80th Street", "81st Street", "82nd Street", "83rd Street", "84th Street", "85th Street", "86th Street", "87th Street", "88th Street", "8th Avenue", "8th Street", "90th Avenue", "90th Street", "9th Avenue", "9th Street", "Abingdon Square", "Adelphi Street", "Albemarle Road", "Albemarle Terrace", "Alexander Avenue", "Amboy Road", "Amity Street", "Amsterdam Avenue", "Andrews Avenue", "Argyle Road", "Arthur Kill Road", "Ashland Place", "Astor Place", "Atlantic Avenue", "Available", "Avenue A", "Avenue Of Americas", "Avenue Of The Americas", "Avenue Z", "Bainbridge Street", "Baltic Street", "Bank Street", "Barclay Street", "Barrow Street", "Baxter Street", "Beach 124th Street", "Beach Street", "Beaver Road", "Beaver Street", "Beck Street", "Bedford Avenue", "Bedford Street", "Beekman Place", "Beekman Street", "Bergen Street", "Berkeley Place", "Bethune Street", "Bicycle Racks", "Bleecker Street", "Bond Street", "Boston Road", "Botanical Garden", "Bowery Street", "Bowling Green", "Bowne Street", "Bridge Street", "Brielle Avenue", "Brighton Beach Avenue", "Broad Street", "Broadway", "Brooklyn Heights", "Broome Street", "Buckingham Road", "Bushwick Avenue", "Cadman Plaza West", "Calyer Street", "Cambridge Place", "Canal Street", "Carlton Avenue", "Carroll Gardens Historic Distr", "Carroll Place", "Carroll Street", "Central Park West", "Central Avenue", "Central Park West", "Central Park North", "Central Park South", "Central Park West", "Centre Street", "Chambers Street", "Charles Street", "Charlton Street", "Chatham Square", "Chauncey Street", "Cheever Place", "Chelsea Square Park", "Christopher Street", "Chruch Street", "Church Avenue", "Church Street", "City College", "City Hall", "Clark Street", "Classon Avenue", "Clay Avenue", "Clermont Avenue", "Clifton Place", "Clinton Avenue", "Clinton Street", "Clove Road", "Coenties Slip", "College Place", "Columbia Heights", "Columbia Place", "Columbia Street", "Columbus Avenue", "Columbus Street", "Commerce Street", "Congress Street", "Convent Avenue", "Cooper Square", "Cornelia Street", "Cornell Cemetery", "Court Place", "Court Square", "Court Street", "Cranberry Street", "Cropsey Avenue", "Crosby Street", "Croton Aqueduct", "Cumberland Street", "Dalny Road", "Dawson Street", "Dean Street", "Decatur Street", "Degraw Street", "Dekalb Avenue", "Ditmars Boulevard", "Ditmas Avenue", "Dorchester Avenue", "Doris Freedman Plaza", "Doughty Street", "Dover Street", "Downing Street", "Drinking Fountain", "Duane Street", "Dyre Avenue", "Eagle Street", "East 103rd Street", "East 104th Street", "East 106th Street", "East 10th Street", "East 110th Street", "East 11th Street", "East 121st Street", "East 125th Street", "East 12th Street", "East 136th Street", "East 139th Street", "East 13th Street", "East 140th Street", "East 141st Street", "East 14th Street", "East 151st Street", "East 156th Street", "East 15th Street", "East 161st Street", "East 166th Street", "East 16th Street", "East 179th Street", "East 17th Street", "East 17th Stret", "East 18th Street", "East 19th Street", "East 20th Street", "East 21st Street", "East 22nd Street", "East 23rd Street", "East 24th Street", "East 29th Street", "East 32nd Street", "East 34th Street", "East 35th Street", "East 36th Street", "East 3rd Street", "East 41st Street", "East 42nd Street", "East 43rd Street", "East 44th Street", "East 45th Street", "East 46th Street", "East 48th Street", "East 49th Street", "East 4th Street", "East 50th Street", "East 52nd Street", "East 53rd Street", "East 54th Street", "East 55th Street", "East 56th Street", "East 57th Street", "East 58th Street", "East 59th Street", "East 60th Street", "East 61st Street", "East 62nd Street", "East 63rd Street", "East 64th Street", "East 65th Street", "East 66th Street", "East 67th Street", "East 68th Street", "East 69th Street", "East 6th Street", "East 70th Street", "East 71st Street", "East 72nd Street", "East 73rd Street", "East 74th Street", "East 75th Street", "East 76th Street", "East 77th Street", "East 78th Street", "East 79th Street", "East 7th Street", "East 80th Street", "East 81st Street", "East 82nd Street", "East 83rd Street", "East 84th Street", "East 85th Street", "East 86th Street", "East 87th Street", "East 88th Street", "East 89th Street", "East 8th Street", "East 90th Street", "East 91st Street", "East 92dnd Street", "East 92nd Street", "East 93rd Street", "East 94th Street", "East 95th Street", "East 96th Street", "East 9th Street", "East Broadway", "East End Avenue", "East Fordham Road", "East Tremont Avenue", "Eastern Parkway", "Edgecombe Avenue", "Eldridge Street", "Elizabeth Street", "Elk Street", "Emmons Avenue", "Ericsson Place", "Everit Street", "Exchange Place", "Faile Street", "Federal Plaza", "Fenimore Street", "Fifth Avenue", "Fillmore Street", "Fiske Place", "Flagg Court", "Flagg Place", "Flatbush Avenue", "Flatbush Malls", "Foley Square", "Fordham University", "Forest Avenue", "Fort Greene Avenue", "Fort Greene Place", "Fort Totten Avenue", "Fort Washington Avenue", "Franklin Avenue", "Franklin Street", "Frederick Douglas Boulevard", "Front Street", "Fulton Ferry Streets", "Fulton Fish Market", "Fulton Landing", "Fulton Street", "Gansevoort Street", "Garden Place", "Garfield Place", "Gates Avenue", "Gateway Boulevard", "Gay Street", "Grace Court", "Grace Court Alley", "Grace Street", "Gracie Mansion", "Gramercy Park East", "Gramercy Park North", "Gramercy Park South", "Gramercy Park West", "Gramercy Place", "Grand Army Plaza", "Grand Avenue", "Grand Central Terminal", "Grand Concourse", "Grand Street", "Grant Street", "Great Jones Street", "Great Kills Road", "Green Wood Cemetery", "Greene Avenue", "Greene Street", "Greenpoint Avenue", "Greenport Road", "Greenwich Avenue", "Greenwich Mews", "Greenwich Street", "Greenwich Village", "Grove Court", "Grove Street", "Guernsey Street", "Hall Street", "Hamilton Avenue", "Hamilton Place", "Hamilton Terrace", "Hanover Square", "Hanson Place", "Harrison Street", "Heberton Avenue", "Henderson Place", "Henry Hudson Parkway", "Henry Street", "Heritage Trail", "Heritage Trails", "Hester Street", "Hewitt Place", "Hicks Street", "High Bridge Aqueduct", "Horatio Street", "Howard Street", "Hoyt Street", "Hubert Street", "Hudson Street", "Hunts Lane", "Hunts Lane Alley", "Hydrants", "Hylan Boulevard", "Independence Avenue", "Irving Place", "Jackson Avenue", "Jackson Street", "Jamaica Avenue", "James Street", "Jane Street", "Jay Street", "Jefferson Avenue", "Jerome Avenue", "John Street", "Jones Street", "Joralemon Street", "Jumel Terrace", "Kane Street", "Kelly Street", "Kenmore Terrace", "Kent Street", "King Street", "Kings Highway", "Kingsbridge Terrace", "Lafayette Avenue", "Lafayette Street", "Laguardia Airport", "Laight Street", "Lefferts Avenue", "Lefferts Homestead", "Lefferts Place", "Lefferts Road", "Lenox Avenue", "Leonard Street", "Leroy Street", "Lewis Avenue", "Lexington Avenue", "Liberty Street", "Lighting", "Lincoln Avenue", "Lincoln Place", "Lincoln Road", "Linwood Street", "Lispenard Street", "Little Bay", "Little Neck Parkway", "Little West 12th Street", "Livingston Street", "Lorillard Snuff Mill", "Lorimer Street", "Louis Street", "Love Lane", "Macdonough Street", "Macdougal Alley", "Macdougal Street", "Macon Street", "Madison Avenue", "Madison Street", "Maiden Lane", "Main Street", "Malcolm X Boulevard", "Manhattan Avenue", "Maple Street", "Marcus Garvey Boulevard", "Marcus Garvey Park", "Marcy Avenue", "Marlborough Road", "Maujer Street", "Mcdonald Avenue", "Mercer Street", "Middagh Street", "Midwood Street", "Milligan Place", "Milton Avenue", "Milton Street", "Mitchell Place", "Monroe Place", "Montague Street", "Montague Terrace", "Montgomery Place", "Morris Avenue", "Morris Park Avenue", "Morton Street", "Mott Street", "Mount Morris Park West", "Mulry Square", "Murray Street", "Narrows Avenue", "Nassau Street", "Nevins Street", "New Dorp Lane", "New Lots Avenue", "Newkirk Avenue", "Nixon Avenue", "Noble Street", "Norfolk Street", "Norman Avenue", "North Moore Street", "Northern Boulevard", "Nostrand Avenue", "Nycta Subway Entrance", "Oak Street", "Ocean Avenue", "Ocean Parkway", "Ogden Avenue", "Old Fulton Street", "Old Slip", "Oliver Street", "Orange Street", "Pacific Street", "Park Avenue", "Park Avenue South", "Park Place", "Park Row", "Park Row North", "Patchin Place", "Pearl Street", "Peck Slip", "Pedestrian Ramps", "Pell Mansion", "Perry Street", "Pierrepont Place", "Pierrepont Street", "Pike Street", "Pineapple Street", "Plaque", "Plaques", "Plaza Street", "Plaza Street West", "Polhemus Place", "Polhemus Street", "Pomander Walk", "Poplar Street", "Powells Cove Boulevard", "President Street", "Prince Street", "Prospect Park West", "Putnam Avenue", "Quincy Street", "Reade Street", "Recycling Cans", "Remsen Avenue", "Remsen Street", "Revere Place", "Richmond Hill Road", "Richmond Road", "Richmond Street", "Richmond Terrace", "Riverdale Avenue", "Riverside Drive", "Riverside Drive West", "Riverside Park", "Rockefeller Center", "Rockefeller Plaza", "Rockland Avenue", "Rogers Avenue", "Roosevelt Avenue", "Roosevelt Island", "Rufus King Manor", "Rugby Road", "Rutherford Place", "Rutland Road", "Ryerson Street", "Sailors' Snug Harbor", "Schermerhorn Street", "Sedgwick Avenue", "Sequine Avenue", "Sheridan Square", "Shore Road", "Sidney Place", "Simpson Street", "Smith Street", "Sniffen Court", "Snug Harbor", "Snug Harbor Cultural Center", "Snyder Avenue", "South 9th Street", "South Elliott Place", "South Oxford Street", "South Portland Avenue", "South Portland Street", "South Street", "South Street Seaport", "South William Street", "Southern Boulevard", "Spring Street", "St. Alban's Place", "St. Andrew's Plaza", "St. Ann's Avenue", "St. Felix Street", "St. James Place", "St. Johns Place", "St. John's Place", "St. Lukes Place", "St. Luke's Place", "St. Mark Avenue", "St. Marks Avenue", "St. Marks Place", "St. Mark's Place", "St. Nicholas Avenue", "St. Nicholas Terrace", "St. Patrick's Place", "St. Paul's Avenue", "State Street", "Sterling Place", "Sterling Street", "Stone Street", "Stratford Road", "Street Signs", "Strong Place", "Stuyvesant Avenue", "Stuyvesant Square", "Stuyvesant Street", "Sullivan Street", "Sutphin Boulevard", "Sutton Place", "Sycamore Avenue", "Sylvan Terrace", "Thomas Street", "Thompson Street", "Throop Avenue", "Times Square", "Tompkins Avenue", "Tompkins Place", "Trinity Place", "Tudor City", "Tudor City Place", "Union Hall Street", "Union Square West", "Union Street", "University Place", "Van Cortlandt Mansion", "Vandam Street", "Vanderbilt Avenue", "Varick Street", "Verandah Place", "Verdi Square", "Vernon Boulevard", "Vesey Street", "Vestry Street", "Village Road South", "Walker Street", "Wall Street", "Warren Place", "Warren Street", "Warsoff Place", "Washington Avenue", "Washington Mews", "Washington Place", "Washington Square North", "Washington Square South", "Washington Square West", "Washington Street", "Water Street", "Watts Street", "Wave Hill", "Waverly Avenue", "Waverly Place", "Webster Avenue", "Weirfield Street", "West 101st Street", "West 102nd Street", "West 105th Street", "West 106th Street", "West 107th Street", "West 108th Street", "West 109th Street", "West 10th Street", "West 114th Street", "West 115th Street", "West 119th Street", "West 11th Street", "West 120th Street", "West 121st Street", "West 122nd Street", "West 123rd Street", "West 125th Street", "West 126th Street", "West 127th Street", "West 128th Street", "West 129th Street", "West 12th Street", "West 130th Street", "West 134th Street", "West 135th Street", "West 138th Street", "West 139th Street", "West 13th Street", "West 140th Street", "West 141st Street", "West 142nd Street", "West 143rd Street", "West 144th Street", "West 145th Street", "West 148th Street", "West 14th Street", "West 150th Street", "West 151st Street", "West 155th Street", "West 156th Street", "West 160th Street", "West 162nd Street", "West 16th Street", "West 17th Street", "West 18th Street", "West 19th Street", "West 20th Street", "West 21st Street", "West 22nd Street", "West 23rd Street", "West 247th Street", "West 249th Street", "West 24th Street", "West 252nd Street", "West 25th Street", "West 261st Street", "West 26th Street", "West 27th Street", "West 29th Street", "West 31st Street", "West 33rd Street", "West 34th Street", "West 37th Street", "West 3rd Street", "West 40th Street", "West 41st Street", "West 42nd Street", "West 43rd Street", "West 44th Street", "West 45th Street", "West 46th Street", "West 47th Street", "West 48th Street", "West 49th Street", "West 4th Street", "West 50th Street", "West 51st Street", "West 52nd Street", "West 53rd Street", "West 54th Street", "West 55th Street", "West 56th Street", "West 57th Street", "West 58th Street", "West 59th Street", "West 5th Street", "West 61st Street", "West 63rd Street", "West 64th Street", "West 65th Street", "West 66th Street", "West 67th Street", "West 68th Street", "West 69th Street", "West 70th Street", "West 71st Street", "West 72nd Street", "West 73rd Street", "West 74th Street", "West 75th Street", "West 76th Street", "West 77th Street", "West 78th Street", "West 79th Street", "West 80th Street", "West 81st Street", "West 82nd Street", "West 83rd Street", "West 84th Street", "West 85th Street", "West 86th Street", "West 87th Street", "West 88th Street", "West 89th Street", "West 8lst Street", "West 8th Street", "West 90th Street", "West 91st Street", "West 92nd Street", "West 93rd Street", "West 94th Street", "West 95th Street", "West 96th Street", "West 9th Street", "West Broadway", "West Drive", "West End Avenue", "West Houston Street", "West Kingsbridge Road", "West Street", "Westervelt Avenue", "Westminster Road", "White Street", "William Street", "Willoughby Avenue", "Willoughby Street", "Willow Place", "Willow Street", "Wilson Avenue", "Winant Place", "Woodrow Road", "Woodycrest Avenue", "Wooster Street", "Worth Street", "Wyckoff Street", "York Avenue" }; #endregion Data Generation public static string Street() { return Streets[Generator.Next(0, Streets.Count - 1)]; } public static string City() { return Cities[Generator.Next(0, Cities.Count - 1)]; } public static string StateName() { return States[Generator.Next(0, States.Count - 1)].StateName; } public static string StateCode() { return States[Generator.Next(0, States.Count - 1)].StateCode; } public static string Zip() { return Zip(false); } public static string Zip(bool withExtension) { var address = States[Generator.Next(0, States.Count - 1)]; return Zip(withExtension, address.ZipRangeBegin, address.ZipRangeEnd); } private static string Zip(bool withExtension, int zipRangeBegin, int zipRangeEnd) { var zip = Generator.Next(zipRangeBegin, zipRangeEnd); var pre = string.Format("{0:d5}", zip); if (!withExtension) return pre; var post = Zip(false); return string.Format("{0}-{1}", pre, post.Substring(0, 4)); } public static string FullAddress() { return FullAddress(false); } public static string FullAddress(bool withExtension) { var address = States[Generator.Next(0, States.Count - 1)]; return string.Format("{0} {1}, {2}, {3} {4}", Generator.Next(1, 10000), Street(), City(), address.StateName, Zip(withExtension, address.ZipRangeBegin, address.ZipRangeEnd)); } public static string PhoneNumber() { return GetRandom.Usa.PhoneNumber(); } public static string SocialSecurityNumber() { return GetRandom.Usa.SocialSecurityNumber(); } internal class UsAddressFake { internal string StateName { get; set; } internal string StateCode { get; set; } internal int ZipRangeBegin { get; set; } internal int ZipRangeEnd { get; set; } } } }
Let’s say that you’ve created a Visual Studio solution with an ASP.NET MVC 3 application, EF Code First and using MvcScaffolding – similar to Steve Sanderson’s Mix 11 cast: Scaffolding – ASP.NET, NuGet, Entity Framework Code First and More. Your MVC 3 project contains the following two Model classes and DbContext classs:
using System; using System.Collections.Generic; namespace MvcCodeFirstSqlCompact.Models { public class Dinner { public int DinnerId { get; set; } public string Title { get; set; } public DateTime EventDate { get; set; } public string Address { get; set; } public string HostedBy { get; set; } public string UrlLink { get; set; } public virtual ICollection<Rsvp> Rsvps { get; set; } } } namespace MvcCodeFirstSqlCompact.Models { public class Rsvp { public int RsvpId { get; set; } public string AttendeeEmail { get; set; } public int DinnerId { get; set; } public virtual Dinner Dinner { get; set; } } } namespace MvcCodeFirstSqlCompact.Models { public class MvcCodeFirstSqlCompactContext : DbContext { public MvcCodeFirstSqlCompactContext() { Database.SetInitializer(new MvcCodeFirstSqlCompactInitializer()); } public DbSet<MvcCodeFirstSqlCompact.Models.Dinner> Dinners { get; set; } public DbSet<MvcCodeFirstSqlCompact.Models.Rsvp> Rsvps { get; set; } } }
using System; using System.Data.Entity; using FizzWare.NBuilder; using FizzWare.NBuilder.Generators; namespace MvcCodeFirstSqlCompact.Models { public class MvcCodeFirstSqlCompactInitializer : DropCreateDatabaseIfModelChanges<MvcCodeFirstSqlCompactContext> { protected override void Seed(MvcCodeFirstSqlCompactContext context) { var uniqueGenerator = new UniqueRandomGenerator(); var generator = new RandomGenerator(); var dinners = Builder<Dinner>.CreateListOfSize(10).WhereAll() .Have(d => d.Title = string.Format("{0} {1} Dinner", uniqueGenerator.Next(0, 100), GetRandom.UK.County())) .And(d => d.Address = UsAddressRandomizer.FullAddress(true)) .And(d => d.HostedBy = string.Format("{0} {1}", GetRandom.FirstName(), GetRandom.LastName())) .And(d => d.EventDate = uniqueGenerator.Next(new DateTime(2010, 1, 1), new DateTime(2011, 5, 4))) .And(d => d.UrlLink = "http://" + GetRandom.Url()) .Build(); foreach (var dinner in dinners) context.Dinners.Add(dinner); var rsvps = Builder<Rsvp>.CreateListOfSize(300).WhereAll() .Have(d => d.AttendeeEmail = GetRandom.Email()) .And(d => d.Dinner = dinners[generator.Next(1, 10)]) .Build(); foreach (var rsvp in rsvps) context.Rsvps.Add(rsvp); } } }
No more manual data insertions into your database once EF Code First creates/recreates the database. Not as sweet as EF Code First combined with MvcScaffolding, but pretty sweet nonetheless?
All pains aside, I’m really impressed with EF Code First and liking it more and more everyday. That said, I hope this post and NBuilder helps your avoid some of the pain associated with EF Code First.
Again, if you are unfamiliar with any of the previous concepts and technologies, I highly recommend that you take a look at Steve Sanderson’s Mix 11 cast - Scaffolding – ASP.NET, NuGet, Entity Framework Code First and More, his blog series - Scaffold your ASP.NET MVC 3 project with the MvcScaffolding package, and explore the usefulness of NBuilder.
Thanks for reading…
No comments:
Post a Comment