Include Grandchildren in EF Query
Update: If you are using Entity Framework Core you should use the following syntax var hierarchy = from p in ctx.Parents .Include(p => p.Children) .ThenInclude(c => c.GrandChild) select p;
Update: If you are using Entity Framework Core you should use the following syntax var hierarchy = from p in ctx.Parents .Include(p => p.Children) .ThenInclude(c => c.GrandChild) select p;
EF Core 3.0 supports entity.HasCheckConstraint() now. To take Ryan’s example: protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<SomeTable>(entity => entity.HasCheckConstraint(“CK_SomeTable_SomeColumn”, “[SomeColumn] >= X”); }
The best way should be using new MigrateDatabaseToLatestVersion initializer. Database.SetInitializer<YourContext>( new MigrateDatabaseToLatestVersion<YourContext, YourMigrationsConfig>()); Database.Initialize(false);
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
If you want to specify the column name and override the property name, you can try the following: Using Annotations public class Job { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Column(“CustomIdName”)] public Guid uuid { get; set; } public int active { get; set; } } Using Code First protected override void OnModelCreating(DbModelBuilder mb) { base.OnModelCreating(mb); mb.Entity<Job>() .HasKey(i => … Read more
One way is to use the Database property off the DbContext: SqlParameter param1 = new SqlParameter(“@firstName”, “Frank”); SqlParameter param2 = new SqlParameter(“@lastName”, “Borland”); context.Database.ExecuteSqlCommand(“sp_MyStoredProc @firstName, @lastName”, param1, param2); EF5 definitely supports that.
ORM (Object Relational Mapper) is a tool that creates layer between your application and data source and returns you the relational objects instead of (in terms of c# that you are using) ADO.NET objects. This is basic thing that every ORM does. To do this, ORMs generally execute the query and map the returned DataReader … Read more
An imperfect solution is to just merge these interfaces you want to persist into base classes and break down the underlying objects with subclasses. EF does support this, and if you go with Table Per Hierarchy (the default), you can sort all of the underlying subclassed objects by a shared property using a regular LINQ … Read more
Unfortunately the answer right now is ‘No’. But you can vote for Better support for default values EDIT 30 Mar 2015: It’s coming in EF7… Support database default values in Code First EDIT 30 Jan 2017: General support for default database values is part of EF Core (the new name for EF7)… Default values EDIT … Read more
Rowan Miller (program manager for Entity Framework at Microsoft) recently posted a good solution to this which uses Interceptors. Admittedly this is only valid in EF 6.1+. His post is about trailing strings in joins, but basically, the solution as applied neatly removes trailing strings from all of the string properties in your models, automatically, … Read more