EF6 ignoring [Table] and [Column] in entity classes

c# ef-code-first entity-framework entity-framework-6 sql-server


I used EF6 Database First tools to generate C# classes for 2 tables from my database, then (as advised in the blog post that helped me through the steps to do that) copied the resulting .cs files into a new project. I made a few edits to the classes to support sensible names in my C# code. Here's a snippet of one of the classes with "LongTableName" replacing a strangely long name used in the database.

namespace RidesData
    public partial class PhoneData
        public int ID { get; set; }

        public int AccountID { get; set; }
        // more fields

I am not in control of the table names, nor the fact that the many of the column names have the table name as prefixes. But the Code First ideas in EF6 should, I thought, let me use reasonable class and field names despite that. (The Database First code generator did a good job of adding code to OnModelCreating to specify that none of the columns corresponding to C# string data used Unicode.)

My model (generated by the EF6 tools and that inherits from DbContext) includes (after some renaming by me)

   public virtual DbSet<PhoneData> PhoneRecs { get; set; }

and I thought all would be fine when I created an instance of PhoneData, populated it, and did


but the first thing that happened when I ran the code -- well before any call to SaveChanges() -- was that EF generated CREATE TABLE statements for the two tables; the table corresponding to the snippet above was named PhoneDatas (not using the specified table name) and the column names were the same as the field names in the class (not what was specified in the Column(...) attributes).

Of course the table I had specified did not need to be created. EF just had to grok that I wanted to use the table and column names I had specified via attributes.

I did not expect this failure of explicit Code First attributes. Does anyone have a clue why this isn't doing what I want, or how to fix it? (Do I have to do something to specify the table & column names in OnModelCreating as well as -- or instead of -- the attributes?)

Note that the project that I copied these classes into had never "seen" the database before. There are no vestiges of any "models" left over from tooling having looked at the database. Also, I hope it does not matter that I've tried to keep things on .Net 4.0 (avoiding going to 4.5 in this code).

Any assistance would be appreciated.

7/23/2014 6:42:35 PM

Accepted Answer

I'm not a big fan of DataAnotations either. Use EntityTypeConfiguration. It gives you the naming flexibility I think you are looking for.


public class PhoneData
   public int ID {get;set;}
   public string SomeProperty {get;set;}

public class PhoneDataMap : EntityTypeConfiguration<PhoneData>
        public PhoneDataMap()
            HasKey(m => m.Id);
            Property(m => m.SomeProperty).HasColumnName("whatever").IsRequired();

Then in your on ModelCreating you add

modelBuilder.Configuration.Add(new PhoneDataMap());

On a side note, if you are having trouble with pluralization of your table names you can add this to OnModelCreating as well

7/24/2014 2:32:45 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow