How can NodaTime be used with EF Code First?

Until custom primitive type persistence is natively supported in Entity Framework, a common work around is to use buddy properties. For each custom primitive within your domain model, you create an associated mapped primitive to hold the value in a format supported by Entity Framework. The custom primitive properties are then calculated from the value … Read more

check constraint entity framework

EF Core 3 To take Ryan’s example: protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<SomeTable>(entity => entity.HasCheckConstraint(“CK_SomeTable_SomeColumn”, “[SomeColumn] >= X”); } Docs. EF Core 7+ protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder .Entity<Blog>() .ToTable(b => b.HasCheckConstraint(“CK_Blog_TooFewBits”, “Id > 1023”)); } See this for more details and changes in 7. Docs.

Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths

All relationships in your model are required because all foreign key properties (CountryId, RegionId, CityId) are not nullable. For required one-to-many relationships EF will enable cascading delete by convention. Country and Region have multiple delete paths to the Store table, for example if you delete a Country the related Stores can be deleted via three … Read more

How do I use Entity Framework in Code First Drop-Create mode?

Use DropCreateDatabaseAlways initializer for your database. It will always recreate database during first usage of context in app domain: Database.SetInitializer(new DropCreateDatabaseAlways<YourContextName>()); Actually if you want to seed your database, then create your own initializer, which will be inherited from DropCreateDatabaseAlways: public class MyInitializer : DropCreateDatabaseAlways<YourContextName> { protected override void Seed(MagnateContext context) { // seed database … Read more

Entity Framework Validation confusion – maximum string length of ‘128’

Default length of string field in code first is 128. If you are using EF validation it will throw exception. You can extend the size by using: [StringLength(Int32.MaxValue)] public string Body { get; set; } This post became somehow popular so I’m adding second approach which also works: [MaxLength] public string Body { get; set; … Read more

Could not load file or assembly ‘EntityFramework’ after downgrading EF 5.0.0.0 –> 4.3.1.0

After failed attempts to find references to EntityFramework in repositories.config and elsewhere, I stumbled upon a reference in Web.config as I was editing it to help with my diagnosis. The bindingRedirect referenced 5.0.0.0 which was no longer installed and this appeared to be the source of the exception. Honestly, I did not add this reference … Read more