Enums with EF code-first – standard method to seeding DB and then using?

Unfortunately, enums are not natively supported on EF 4.1. Here’s one rather known article on how to deal with them: Faking enums on EF 4. It does, however, require a wrapper. There’s a simpler way to map enums in EF 4 however: just create an int property on your class to represent the int value … Read more

How to Specify Primary Key Name in EF-Code-First

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

How can set a default value constraint with Entity Framework 6 Code First?

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

Mocking or faking DbEntityEntry or creating a new DbEntityEntry

Just like the other case, what you need is to add an additional level of indirection: interface ISalesContext { IDbSet<T> GetIDbSet<T>(); void SetModified(object entity) } class SalesContext : DbContext, ISalesContext { public IDbSet<T> GetIDbSet<T>() { return Set<T>(); } public void SetModified(object entity) { Entry(entity).State = EntityState.Modified; } } So, instead of calling the implementation, you … Read more

Entity Framework Code First : Setting up One-To-One foreign key association using Annotations

One-to-one foreign key associations are not supported by Entitiy Framework. You must remove the foreign key and use shared primary keys (primary key of the dependent is its foreign key to the principal at the same time): public class StandardRack { public int Id {get;set} public StandardRelay StandardRelay {get;set} } public class StandardRelay { public … Read more

How do I define a database view using Entity Framework 4 Code-First?

That’s because you cannot define database view using code-first approach. Database view is database construct which uses SQL Query on top of existing tables / functions. You can’t define such constructs using code first. If you want view you must create it manually by executing CREATE VIEW SQL script for example in custom initializer – … Read more

Optimistic concurrency: IsConcurrencyToken and RowVersion

Both in EF6 and EF-core, when working with SQL Server, you have to use this mapping: modelBuilder.Entity<Product>() .Property(t => t.RowVersion) .IsRowVersion(); // Not: IsConcurrencyToken IsConcurrencyToken does configure a property as concurrency token, but (when using it for a byte[] property) the data type is varbinary(max) its value is always null if you don’t initialize it … Read more

Modelling polymorphic associations database-first vs code-first

I personally stick with Database first when using EF on any schema that is this level of complexity. I have had issues with complex schemas in regards to code first. Maybe the newer versions are a little better, but worrying how to try and code complex relationships seems less straight forward then allowing the engine … Read more

How do you ensure Cascade Delete is enabled on a table relationship in EF Code first?

Possible reason why you don’t get cascading delete is that your relationship is optional. Example: public class Category { public int CategoryId { get; set; } } public class Product { public int ProductId { get; set; } public Category Category { get; set; } } In this model you would get a Product table … Read more

Navigation Property without Declaring Foreign Key

I believe, it is not possible to define the relationship only with data attributes. The problem is that EF’s mapping conventions assume that Creator and Modifier are the two ends of one and the same relationship but cannot determine what the principal and what the dependent of this association is. As far as I can … Read more