Entity Framework 4.1 code first mapping to already existing database table?

You need to instruct EF to ignore IsActive property and map other properties. If you don't like data annotations you can do this with fluent API.

Up vote 0 down vote favorite 1 share g+ share fb share tw.

I am using Entity Framework 4.1 code first to connect to an already existing database. The table that I am using first is called Bank. I also have a Bank class as my domain model.

This is how I mapped my class and table: public class HbfContext : DbContext { public DbSet Banks { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity(). ToTable("Bank"); } } My Bank table: BankID INT BankName 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 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?

Entity-framework entity-framework-4 entity-framework-4.1 ef-code-first code-first link|improve this question edited Jan 13 at 11:03 asked Jan 13 at 10:36Brendan Vogt1,3851825 92% accept rate.

You need to instruct EF to ignore IsActive property and map other properties. If you don't like data annotations you can do this with fluent API: modelBuilder.Entity(). Ignore(b => b.

IsActive); modelBuilder.Entity(). Property(b => b. Id).

HasColumnName("BankID"); modelBuilder.Entity(). Property(b => b. Name).

HasColumnName("BankName").

There is no Id and Name columns as you can see in my table structure. So instead of change the domain model's properties would it maybe be wise to map the column names to the properties via fluent API? – Brendan Vogt Jan 13 at 10:58 I missed this part.

If your class declaration differs from your table you must map all differences. I modified my sample. – Ladislav Mrnka Jan 13 at 11:02 Thanks Ladislav.

– Brendan Vogt Jan 13 at 11:05.

Try adding the following attribute to the IsActive property and see if it helps NotMapped UPDATE Use Data Annotations instead of fluent API which is easier to grasp and use IMO First of all remove the OnModelCreating function from your context class Then Define your model like this Table("Bank") public class Bank { key public int Id { get; set; } Column("BankName") public string Name { get; set; } NotMapped public bool IsActive { get; set; } } This shall work as expected.

I can't see how this will help if the column names in the select don't correspond the column names in the table. – Brendan Vogt Jan 13 at 10:45 This is to indicate that the said property doesn't need to be mapped and doesn't exist in the database....Try this, it will solve your problem – Pankaj Upadhyay Jan 13 at 10:48 The other thing you need to to do is change your Name property to BankName to make sure it matches the corresponding column – Pankaj Upadhyay Jan 13 at 10:49 Instead of changing my domain names properties, can't I map the columns to properties using fluent API? – Brendan Vogt Jan 13 at 10:54 You can do that but you haven't done anything other than to map your table and Model class...That's why its not able to recognize Whats Name, BankName and other stuff.... – Pankaj Upadhyay Jan 13 at 10:56.

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; }.

– Brendan Vogt Jan 13 at 10:46 yes I think so, try adding something like modelBuilder.Entity(). Property(bank => bank. ID).

HasColumnName("BankID"); to your DbContext OnModelCreating override. – The Ed R Jan 13 at 11:21.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions