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