An attempt to sow data in an ASP.NET web application using the Entity Framework and code first. I put this code in the Seed() method of the Configuration.cs file. I am currently working on the address aspects of the solution, and I am trying to load all the states and counties that are uploaded to the system.
Here is my state code that works great ...
context.StateProvinces.AddOrUpdate( p => p.Abbreviation, new StateProvince { Abbreviation = "AL", Name = "Alabama" }, new StateProvince { Abbreviation = "AK", Name = "Alaska" }, new StateProvince { Abbreviation = "AZ", Name = "Arizona" }, .... );
How can I use the AddOrUpdate() function for context.Counties ? This is what my County facility looks like.
public class County { public int CountyId { get; set; } public int StateProvinceId { get; set; } public String Name { get; set; } [ForeignKey("StateProvinceId")] public virtual StateProvince StateProvince { get; set; } }
Although I can simply duplicate the format of what I did with the states, we know that country names are reused throughout the country. Therefore, I cannot just use c => c.Name as an existing check. Could I use the name and field of StateProvinceId?
c # entity-framework ef-code-first
RSolberg
source share