EntityFramework Seed AddOrUpdate with Foreign Key

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?

+7
c # entity-framework ef-code-first
source share
2 answers

Why not add counties to states and not do it in reverse order?

If you cannot do this, you can change your value as follows:

 var al = new StateProvince { Abbreviation = "AL", Name = "Alabama" }, var ak = new StateProvince { Abbreviation = "AK", Name = "Alaska" }, var ar = new StateProvince { Abbreviation = "AZ", Name = "Arizona" }, context.StateProvinces.AddOrUpdate( p => p.Abbreviation, al, ak, ar ); context.SaveChanges(); context.Counties.Add(new County() { Name = "Something", StateProvince = al }); // DONT CALL SAVE, Initializer needs something to do or it complains 
+7
source share

You will need to use the OnModelCreating () method and declare a composite key that contains the county name and state abbreviation. You will also need a property for your county to reduce.

This is a piece of code with an example:

 public class DirectMailDbContext : DbContext, IDirectMailDbContext { public DbSet<BulkQualificationCriteria> QualificationRanges { get; set; } public DbSet<CarrierRouteDefinition> RouteDefinitions { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer(new DirectMailDbContextDropInitializer()); modelBuilder.Entity<BulkQualificationCriteria>().HasKey(q => new {q.QualificationLevel, q.MatchType}); // <- Define composite primary key here modelBuilder.Entity<CarrierRouteDefinition>() .HasKey(c => new {c.Zip, c.RouteNumber}); base.OnModelCreating(modelBuilder); } 

FROM OP

 public class County { public int CountyId { get; set; } public int StateProvinceId { get; set; } public String Name { get; set; } [ScaffoldColumn(false)] public String StatePostalAbbreviation { get; set; } [ForeignKey("StateProvinceId")] public virtual StateProvince StateProvince { get; set; } } 

SEED

 context.Counties.AddOrUpdate( c => new {c.StatePostalAbbreviation, c.Name}, new County { Name = "Aleutians East", StatePostalAbbreviation = "AK" }, new County { Name = "Aleutians West", StatePostalAbbreviation = "AK" }, .... ); 

Onmodelcreating

 modelBuilder.Entity<County>().HasKey(q => new { q.Name, q.StatePostalAbbreviation }); 
+2
source share

All Articles