locked
BUG: Entity Framework 6 Beta - Exception When Attempting To Map Table With Column Name The Same As The Table Name RRS feed

  • Question

  • It may sound pretty stupid to have a column with the same name as its table...  But that's the way it is in production, so that's what we have to build our app to map to.  This bug doesn't happen in EF5.  Would like to upgrade to EF6 to stay ahead of the curve ;)

    Example:

    using System;
    using System.Linq;
    using System.Data.Entity;
    using System.Data.Entity.ModelConfiguration;
    
    namespace EF_Table_Column_Name_Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                Database.SetInitializer(new NullDatabaseInitializer<MyContext>());
                var context = new MyContext();
                var topic1 = context.Topics.FirstOrDefault();
    
                Console.WriteLine(topic1.Title);
            }
    
            public class Topic
            {
                public int Id { get; set; }
                public string Title { get; set; }
                public string Context { get; set; }
                public string SubjectMatter { get; set; }
            }
    
            public class TopicConfiguration : EntityTypeConfiguration<Topic>
            {
                public TopicConfiguration()
                {
                    Property(topic => topic.Title).HasColumnName("Topic");
                }
            }
    
            public class MyContext : DbContext
            {
                public MyContext() : base("Name=TestDatabase")
                {
                    
                }
                public DbSet<Topic> Topics { get; set; }
    
                protected override void OnModelCreating(DbModelBuilder modelBuilder)
                {
                    modelBuilder.Configurations.Add(new TopicConfiguration());
    
                    base.OnModelCreating(modelBuilder);
                }
            }
        }
    }
    

    App.config File:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
      <connectionStrings>
        <add name="TestDatabase" providerName="System.Data.SqlClient" connectionString="Server=AGILLIAM-LAP\LOCALHOST; Database=Test; Trusted_Connection=True;"/>
      </connectionStrings>
    </configuration>

    Database Table:

    Please don't make it an architectural rule that this can't happen!!!!  Hope this gets fixed by production!

    Cheers,

    -Anthony

    Wednesday, June 5, 2013 6:30 PM

Answers

  • Hi Anthony,

    Thanks for your feedback.

    I can reproduce the same problem with EF6 beta1. A ModelValidationException is thrown at run time. I'm afraid the validation is by design. You can also try to give your valuable feedback about EF6 in CodePlex.

    Currently the workaround I can think out is to change your Topic class to some others names like MyTopic, and then use ToTable() method to configure the table name in database.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Web Dev Tony Thursday, June 20, 2013 3:48 PM
    Monday, June 10, 2013 3:45 AM