Here is what I am currently doing:
modelBuilder.Entity<Product>().Property(e => e.Name).IsRequired(); modelBuilder.Entity<Product>().Property(e => e.UPC).IsRequired(); modelBuilder.Entity<Product>().Property(e => e.Price).IsRequired(); modelBuilder.Entity<Product>().Property(e => e.Description).IsRequired();
Here is what I would like to be doing:
modelBuilder.Entity<Product>() .Property(e => e.Name).IsRequired() .Property(e => e.UPC).IsRequired() .Property(e => e.Price).IsRequired() .Property(e => e.Description).IsRequired()
The latter, though, doesn't work. Is there another way not to have to repeat the modelBuilder.Entity<Product>()
each time?
Here is the current most pithy option:
var e = modelBuilder.Entity<Product>(); e.Property(e => e.Name).IsRequired(); e.Property(e => e.UPC).IsRequired(); e.Property(e => e.Price).IsRequired(); e.Property(e => e.Description).IsRequired();
This is compatible with all of the existing DbModelBuilder extension methods since it's just adding a fluent layer on top, but that it does come with some syntactical overhead. Not exactly what you asked for, but doesn't involve mucking around with the supporting code. Haven't fully tested this yet, but it should work if you're comfortable with the syntax:
OR
Supporting code: