---
title: "Entity Framework 4.1 code first mapping to already existing database table"  
description: "Entity Framework 4.1 code first mapping to already existing database table"  
author: "Anonymous User"  
published: 2013-09-28  
updated: 2013-09-28  
canonical: https://www.mindstick.com/forum/1559/entity-framework-4-1-code-first-mapping-to-already-existing-database-table  
category: "ado.net"  
tags: ["ado.net"]  
reading_time: 2 minutes  

---

# Entity Framework 4.1 code first mapping to already existing database table

I am using [Entity Framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach) 4.1 code first to [connect](https://www.mindstick.com/articles/12810/how-to-connect-tablet-to-external-monitor-or-flat-screen-tv-using-computer-adapters) to an [already](https://yourviews.mindstick.com/view/88453/the-hidden-ways-ai-is-already-controlling-your-daily-life) [existing database](https://www.mindstick.com/forum/159905/how-can-i-generate-migrations-for-an-existing-database-in-dot-net-6). The [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) that I am using first is called Bank. I also have a Bank [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) as my [domain](https://www.mindstick.com/articles/12298/how-to-select-seo-friendly-domain-names-for-your-new-website) model. This is how I mapped my class and table:\

\

```
public class HbfContext : DbContext
{
     public DbSet<Bank> Banks { get; set; }
     protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
          modelBuilder.Entity<Bank>().ToTable("Bank");
     }
}
```

\

## My Bank table:

```
BankID INTBankName VARCHAR(50)
```

\

My Bank class looks like this:

```
public class Bank
{
     public int Id { get; set; }
     public string Name { get; set; }
     public bool IsActive { get; set; }
}
```

I am having [issues](https://www.mindstick.com/articles/12944/mattresses-for-kids-and-mothers-should-meet-certain-criteria-issues-you-should-consider) when I want to return all the banks. The SQL statement returned from:\

```
return db.Banks
     .OrderBy(x => x.Name);
```

is:

```
SELECT
     [Extent1].[Id] AS [Id],
     [Extent1].[Name] AS [Name],
     [Extent1].[IsActive] AS [IsActive]
FROM
     [dbo].[Bank] AS [Extent1]
ORDER BY
     [Extent1].[Name] ASC
```

\

This is not going to work because my table does not have the Id, Name and IsActive columns. How would I fix this and would EF map BankId to Id and **BankName** to Name automatically?

## Replies

### Reply by Anonymous User

Hi Pravesh!

You could use System.ComponentModel.DataAnnotations to map the property names to match those in your existing table e.g.

```
    [Column("BankID")]
    [Required]
    public string Id
    {
        get;
        set;
    }
```

\


---

Original Source: https://www.mindstick.com/forum/1559/entity-framework-4-1-code-first-mapping-to-already-existing-database-table

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
