using Guid as PK with EF4 Code First

Have you set Identity StoreGeneratedPattern? You can do it in the OnModelCreating method: modelBuilder.Entity<Foo>().Property(o => o.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); or using the DataAnnotation attributes: public class Foo { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id {get;set;} public string Name {get;set;} }

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

Asp.Net Web API Error: The ‘ObjectContent`1’ type failed to serialize the response body for content type ‘application/xml; charset=utf-8’

I would suggest Disable Proxy Creation only in the place where you don’t need or is causing you trouble. You don’t have to disable it globally you can just disable the current DB context via code… [HttpGet] [WithDbContextApi] public HttpResponseMessage Get(int take = 10, int skip = 0) { CurrentDbContext.Configuration.ProxyCreationEnabled = false; var lista = …

Read more

Entity Framework/SQL2008 – How to Automatically Update LastModified fields for Entities?

I know I’m a little late to the party, but I just solved this for a project I’m working on and thought I’d share my solution. First, to make the solution more re-usable, I created a base class with the timestamp properties: public class EntityBase { public DateTime? CreatedDate { get; set; } public DateTime? …

Read more