0

I have the following problem: I have a hierarchy of entities that uses inheritance. I have a two identical fields( the name and type is the same ) in two of the sub-entities. When I try to "Update-Database -Force" on the project EF5 complains that there are there is already a column with name X.

The way EF5 generates the tables is that it actually generates single table and puts there all the fields of the base entity plus the all the fields of the derived entities.

Is there a way to force a different database column name from the property name. Are there any other solutions( I know it might be architectural problem to duplicate data but making this common will introduce more complex database hierarchy that I don't want to use ).

Thanks:)

1
  • Is this still a problem for you? Do you need more information than the answers provide? Commented Dec 19, 2013 at 18:46

2 Answers 2

2

This can be done in one of two ways, either using Fluent API or property attributes on your class properties.

[Column("ColumnName")]
public string PropertyName
{
   get;
   set;
}

See MSDN - ColumnAttribute Class for more details on the column attribute.

Otherwise, use Fluent API. Within your context class-

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   base.OnModelCreating(modelBuilder);

   modelBuilder.Entity<YourClass>().Property(yc => yc.PropertyName).HasColumnName("ColumnName");

See MSDN - HasColumnName extension method for more on this method.

The article linked by Baximilian will be useful in learning more about this.

Sign up to request clarification or add additional context in comments.

Comments

0

If I understand you correctly, you need to change column name in DB for field, so you can use ColumnAttribute.

You can find more information here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.