0

Can entity framework ignore the fact that I am implementing interfaces in an entity? The situation is that I have a Tag class that implements IModelObject interface which defines that every class that implements it needs to have Id property. The class is below:

public class Tag : IModelObject, IEquatable<Tag>
{
    [ScaffoldColumn(false)]
    public virtual int Id
    {
        get;
        set;
    }

    [Required]
    public virtual string Name
    {
        get;
        set;
    }...

}

And every entity in my domain model implements IModelObject. There is no base class, just an interface. Default mapping works, but in the database Discriminator column is added. And this Discriminator is not part of my domain model so I don't need it in my database.
Implementing an interface is not an inheritance, I am also implementing an IEquatable interface. So why does Entity Framework acts like this is inheritance and adds Discriminator column and how can I avoid this by not using base or abstract classes?

2
  • Does the [NotMapped] attribute work for you? You'd have to set that on every property defined by the interface(s) in every implementation of the interface... Commented Sep 19, 2012 at 22:27
  • modelBuilder.Ignore<MissingTag> solved the issue, and I didn't mention the MissingTag class in the question because i forgot or didn't think was relevant for this issue. Commented Sep 19, 2012 at 22:57

1 Answer 1

1

I'm a little confused by your reasoning. Inheritence has nothing to do with your problem. Your problem is that your entity contains properties (and virtual ones at that. EF does special things with virtual properties). EF will create a column for every public property on your entity. I'm not sure why you think that it shouldn't, as that's what it does.

If you want EF to ignore a property, then you can use the [NotMapped] attribute on the property, and EF will ignore it.

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

2 Comments

The thing that it created a column for every public property is fine. But it also added Discriminator column, EF acted like it is Table per Hierarchy mapping. I have managed to solve the problem and you were right, it had nothing to do with interface. The problem was that I was trying to implement Special Case pattern (martinfowler.com/eaaCatalog/specialCase.html) and have MissingTag object instantiated and returned if e.g. no Tag with requested id was found in database.And that was the problem, because it detected that MissingTag inherits from Tag, and I missed that fact.
Virtual properties were also not the problem. I have solved everything by adding modelBuilder.Ignore<MissingTag>. I need virtual properties to return special values of Id and Name.

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.