user2184057
user2184057

Reputation: 964

BaseEntity in Entity Framework Model First

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?

Upvotes: 0

Views: 3070

Answers (4)

user2184057
user2184057

Reputation: 964

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;
        }
    }

Upvotes: 0

m.casey
m.casey

Reputation: 2599

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

Upvotes: 1

DavidG
DavidG

Reputation: 118937

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; }
}

Upvotes: 1

Alexander Smirnov
Alexander Smirnov

Reputation: 1655

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

Upvotes: 1

Related Questions