Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 4 - EF Model w/ Spaces

I'm developing a project in C# / ASP.NET MVC 4 / EF5. The existing database has spaces in the column names, which I've never come across, don't have the authority to change and have no idea how to workaround.

I've tried lots of stuff to get the column name to be read, including below, but can't figure it out. Here is a simplified version of the code with something I tried but didn't work (I'm happy to provide more code if needed):

using System;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Data.Linq.Mapping;

namespace Domain.Entities
{
    public class TheTable
    {
        public int id { get; set; }

        [Column(Name="The Column Name")]
        public int TheColumnName { get; set; } 
    }
}

I always get this as a result:

Invalid column name 'TheColumnName'.

BTW, I can successfully connect to all columns that don't have spaces. Thanks for any help!

like image 446
edward Avatar asked Oct 04 '22 08:10

edward


1 Answers

Thanks to Kirill, here is the answer for people having this problem in the future:

modelBuilder.Entity<TheTable>().Property(p => p.TheColumnName).HasColumnName("The Column Name");
like image 156
edward Avatar answered Oct 13 '22 10:10

edward