user1391184
user1391184

Reputation: 11

How to map column names with EF 4.3.1?

I've got problem with simple mapping (using EF 4.3.1 - code first approach)

public class Someclass
    {
        public virtual int ID { get; set; }
        public virtual string Name { get; set; }
    } 

And Table someclass with int ID and varchar someclass_name. Now what I want to do is map Name with someclass_name

 modelBuilder.Entity<Someclass>()
             .Property(r => r.Name).HasColumnName("someclass_name");

But id doesn't work and exception says: "context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269)."

I've also tried doing that by:

modelBuilder.Configurations.Add(new SomeclassMap());

public class SomeclassMap : EntityTypeConfiguration<Someclass>
    {
        public SomeclassMap() {
          //  this.Property(r => r.Name).HasColumnName("someclass_name");
            Map(r =>
            {
                Property(m => m.Name).HasColumnName("restaurant_name");
            });
        }
    }

Can somebody tell me what am I doing wrong? THX

Upvotes: 1

Views: 721

Answers (4)

user1391184
user1391184

Reputation: 11

I found the solution:

Database.SetInitializer<SomeDB>(null);

We need to turn of database context checking.

Upvotes: 0

user1391184
user1391184

Reputation: 11

I installed EF 5 and had this same problems. what I "discovered" is that

[Column("Name")]
public string Name1 { get; set; }

means that Name1 points to field "Name" in DB. So I didnt change field name, I changed property name in my class and I let EF know that this "Name1" points to "Name". It's a bit strange approach but it works this way. DB probably writes down somewhere DB hash and checks if it change. But I don't know yet where to find it.

Upvotes: 0

James Manning
James Manning

Reputation: 13589

Not sure if it'll avoid the migration notification or not, but you could try adding [Column("someclass_name")] to your Name property.

See the section named Map a Property to a Column with a Different Name

Upvotes: 0

1tufgt
1tufgt

Reputation: 35

You will want to use EF 4.3's migration utility. The error is notifying you that the model has changed since the database has been built and needs to be updated. Check out this link: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx

Upvotes: 1

Related Questions