Possible to default DateTime field to GETDATE() with Entity Framework Migrations?

You can use DateCreated = c.DateTime(nullable: false, defaultValueSql: “GETDATE()”) Usage: public partial class MyMigration : DbMigration { public override void Up() { CreateTable(“dbo.Users”, c => new { Created = c.DateTime(nullable: false, defaultValueSql: “GETDATE()”), }) .PrimaryKey(t => t.ID); … Update 2012-10-10: As requested by Thiago in his comment, I add a little extra context. The code … Read more

Entity Framework loading child collection with sort order

You cannot achieve it directly because neither eager or lazy loading in EF supports ordering or filtering. Your options are: Sort data in your application after you load them from database Execute separate query to load child records. Once you use separate query you can use OrderBy The second option can be used with explicit … Read more

Calculated column in EF Code First

You can create computed columns in your database tables. In the EF model you just annotate the corresponding properties with the DatabaseGenerated attribute: [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public double Summ { get; private set; } Or with fluent mapping: modelBuilder.Entity<Income>().Property(t => t.Summ) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed) As suggested by Matija Grcic and in a comment, it’s a good idea to make … Read more

Order navigation properties when using Include and/or Select methods with EF 4.1 Code-First?

Unfortunately eager loading (Include) doesn’t support any filtering or sorting of loaded child collections. There are three options to achieve what you want: Multiple roundtrips to the database with explicite sorted loading. That’s the first code snippet in your question. Be aware that multiple roundtrips are not necessarily bad and that Include and nested Include … Read more

How to declare one to one relationship using Entity Framework 4 Code First (POCO)

Three methods: A) Declare both classes with navigation properties to each other. Mark one of the tables (the dependent table) with the ForeignKey attribute on its Primary Key. EF infers 1-to-1 from this: public class AppUser { public int Id { get; set; } public string Username { get; set; } public OpenIdInfo OpenIdInfo { … Read more

EF4 Code First: how to add a relationship without adding a navigation property

I believe you always need navigation property on at least one side when using code-first. Then you will be able to map it: public class User { public string UserId { get; set; } public string PasswordHash { get; set; } public bool IsDisabled { get; set; } public DateTime AccessExpiryDate { get; set; } … Read more

Debug code-first Entity Framework migration codes

I know that EF Code First Migrations is relatively new tool but don’t forget about you are still in .NET. So you can use: if (System.Diagnostics.Debugger.IsAttached == false) { System.Diagnostics.Debugger.Launch(); } After that you can see your InnerException. Or you can use try…catch statement like this: Exception handling Entity Framework