0

I have a small problem with EF Model First. I wanna have one generic entity not mapped to any database table. Entity with only one property Int Id. And all other entities should derive from this one. (Thanks to that base class i will be able to make very generic repository).

I already have all my entities except that base (till now each of them had their own key property).

When i try to Add my BaseEntity i get error:

Problem in mapping fragments starting at line 614:Must specify mapping for all key properties (BaseEntities.Id) of the EntitySet BaseEntities.
...\DataAccess\Model.edmx

No matter if BaseEntity is set to abstract or not.

The same errors appears again with any other Entity that derives from that one. In Code First this would be very simple but i have no idea how to do it in a good way with Model First.

What am I doing wrong?

4 Answers 4

1

Try to override this behaviour using FluentAPI:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Ignore<MyBaseClass>();
}

Also may be this approach will work for you: Ignore base class / interfaces completely in entity framework 5 code first

1
  • FluentApi was rather for Code First (at least this is what i thought) Where should i put this code? Jul 22, 2014 at 14:11
1

You can either ignore it in the OnModelCreating method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Ignore<BaseEntity>();
}

Or if you are using code first, add [NotMapped] attribute to the class:

[NotMapped]
public class BaseEntity
{
    public int Id { get; set; }
}
1
  • But i use EDMX so OnModelCreating is never called. Guess i will need to work a little with T4 template. Jul 22, 2014 at 14:29
1

I believe what you want is called a Table Per Hierarchy. A tutorial for implementing it can be found here.

In short, in the designer, the following steps are necessary:

  • Set BaseEntity to Abstract == true
  • Set your related items to Abstract == false and their Base Type to BaseEntity
0

Well did it in my own way. I have modified Model.tt and changed 2 functions

public string EntityClassOpening(EntityType entity)
    {
        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1}partial class {2}{3}",
            Accessibility.ForType(entity),
            _code.SpaceAfter(_code.AbstractOption(entity)),
            _code.Escape(entity),
            _code.StringBefore(" : ", "BaseEntity"));

 public string Property(EdmProperty edmProperty)
    {
        if (edmProperty.Name != "Id")
        {
            return string.Format(
                CultureInfo.InvariantCulture,
                "{0} {1} {2} {{ {3}get; {4}set; }}",
                Accessibility.ForProperty(edmProperty),
                _typeMapper.GetTypeName(edmProperty.TypeUsage),
                _code.Escape(edmProperty),
                _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
                _code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
        }
        else
        {
            return String.Empty;
        }
    }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.