سؤال

I have a C# project that uses the EF CodeFirst approach. My problem is how EF is interpreting my classes and generating the database tables. EF is inferring too many things and my resulting db is not the way I would like. Specifically, it is generating additional id columns in one of my mapping classes.

Here are my POCO classes:

    public partial class Attribute
    {
    public int Id {get;set;}
    public string Name {get;set;}
    public virtual ICollection<EntityAttribute> EntityAttributes {get;set;}
    }

    public partial class Grant
    {
    public int Id {get;set;}
    public string Name {get;set;}
    public virtual ICollection<EntityAttribute> EntityAttributes {get;set;}
    }

    public partial class Donor
    {
    public int Id {get;set;}
    public string Name {get;set;}
    public virtual ICollection<EntityAttribute> EntityAttributes {get;set;}
    }

    public enum EntityTypeEnum
    {
    Grant = 1,
    Donor = 2
    }

    public partial class EntityAttribute
    {
    public int Id {get;set;}
    public int EntityId {get;set;}
    public int AttributeId {get;set;}
    public int EntityTypeId {get;set;}
    public EntityTypeEnum EntityType
    {
       get{return (EntityTypeEnum)this.EntityTypeId;}
       set{this.EntityTypeId = (int)value;}
    }
    public virtual Grant Grant {get;set;}
    public virtual Donor Donor {get;set;}
}

My mapping classes are typical but here is the EntityAttributeMap class:

public partial class EntityAttributeMap : EntityTypeConfiguration<EntityAttribute>
{
public EntityAttributeMap()
{
this.ToTable("EntityAttribute");
this.HasKey(ea => ea.Id);
this.Property(ea => ea.EntityTypeId).IsRequired();
this.Ignore(ea => ea.EntityType);

this.HasRequired(ea => ea.Grant)
    .WithMany(g => g.EntityAttributes)
    .HasForeignKey(ea => ea.EntityId);

this.HasRequired(ea => ea.Donor)
    .WithMany(d => d.EntityAttributes)
    .HasForeignKey(ea => ea.EntityId);

this.HasRequired(ea => ea.Attribute)
    .WithMany(a => a.EntityAttributes)
    .HasForeignKey(ea => ea.AttributeId)
}
}

All of my unit tests perform as expected. However, the table EntityAttribute gets rendered with DonorId and GrantId columns. I don't want this as I actually have dozens of other "EntityTypes" that will be used for this scenario. That is why I chose the EntityTypeEnum class.

What am I doing wrong? Or is there another way I should be mapping these so EF handles things the way I want. Thanks.

هل كانت مفيدة؟

المحلول

The EF doesn't support enums at all, as of V4 CTP 5. They might be included in the next release.

Having said that, the schema looks (to me; it's not clear from your post, and your intentions may be different) too close to an EAV for my comfort. For the usual reasons (Google it) I dislike these, and wouldn't want that sort of model even with enum support in the EF.

Why not map to another entity type instead of an enum?

If you ask a question in the form of "Here are my business needs; what is the best schema for this?" you may get a better answer.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top