Razi Syed
Razi Syed

Reputation: 289

EF 4.2 Ignore a single property in code first mapping to a complex type

I'm trying to implement a relatively simple concept, but looks like EF code first doesn't have an easy way to do this. Basically I have an address class, which also has a County property. I'm mapping to existing tables. In some cases, the table has a field for county, and in others it does not. So I'm simply trying to ignore County where it doesn't apply.

Fluent API allows mapping to a Complex Type's property:

.Property(Function(c) c.HomeAddress.County).HasColumnName("COUNTY") 

Thats great. But apparently it doesn't allow you to ignore it:

.Ignore(Function(c) c.HomeAddress.County)

Throws the error:

The expression 'p => p.Address.County' is not a valid property expression. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'.

This really should be allowed in Fluent API and should be as simple as the above statement.

So by convention, Code first thinks it's a Property, and you get the error:

"Invalid column name 'Address_County'.

This really sucks! So I'm basically stuck with this property. How do I ignore it??? Any ideas?

I don't want to remove the property, and I cannot add it to the table. I tried adding the annotation, and the code works, but then I cannot map it in cases where I need it.

I hope there is an easy solution, or at least an adequate work around.

Upvotes: 2

Views: 2995

Answers (3)

Sean M
Sean M

Reputation: 626

You can ignore complex type properties in 6.1.1 (Don't know exactly when it started getting support)

public class MyDbContext
        : DbContext
{
//...
   protected override void OnModelCreating(DbModelBuilder modelBuilder)
   {
      modelBuilder
         .ComplexType<[complex-type-class]>()
         .Ignore(x => x.[member-to-ignore]);

}
//...
}

Upvotes: 6

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364409

That is not possible and currently it is by design. EF code first still uses EDM inside and EDM has very restrictive rules.

  • You can define complex type but all its properties become part of the model
  • Every property of entity type or complex type which is part of the model must be mapped

The complex type definition itself will add it to the model but the mapping can only point the defined property to correct database column. It cannot ignore defined property (because it must be mapped). The only way to ignore the property is in complex type definition but in such case your property will never be mapped.

Conclusion: Once you are using complex type you must have all properties mapped in all usages of the type. If you have single case very any property of the complex type cannot be mapped that property must be either removed from the complex type in all cases or you cannot use that complex type.

Upvotes: 1

cincura.net
cincura.net

Reputation: 4160

Although I'm not 100% sure, I think you can't ignore just part of complex type. But you can either create two different complex types (because that's actually what really this case is), maybe EF would be OK with deriving one from the other (not sure). Or create a view, that will have this column, but with NULL (that's the default value for string anyway).

Upvotes: 1

Related Questions