开发者

EF CTP5 mapping problem when using a Entity base class

I'm having an odd problem with many-to-many relationships in EF CTP5 when a base class is involved. I'll first show you a simple mapping that works.

I have the following two classes (entities):

public class Product
{
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Process> Processes { get; set; }
}

public class Process
{
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Product> Products { get; set; }
}

And my mapping classes:

public class ProductMapping : EntityTypeConfiguration<Product>
{
    public ProductMapping()
    {
        ToTable("Products");
        HasKey(t => t.ID);

        Property(t => t.ID).HasColumnName("product_id");
        Property(t => t.Name).HasColumnName("name");
    }
}

public class ProcessMapping : EntityTypeConfiguration<Process>
{
    public ProcessMapping()
    {
        ToTable("Processes");
        HasKey(t => t.ID);

        Property(t => t.ID).HasColumnName("process_id");
        Property(t => t.Name).HasColumnName("name");

        HasMany(p => p.Products)
            .WithMany(p => p.Processes)
            .Map(m =>
            {
                m.ToTable("Product_processes");
                m.MapLeftKey(process => process.ID, "process_id");
                m.MapRightKey(product => product.ID, "product_id");
            });     
    }
}

This mapping works perfectly. However, I want to introduce a base class for my entities. So as a start, I created the following base class:

public abstract class Entity
{
    public int ID { get; set; }
}

I made my two entities Product and Process inherit from this Entity base class and of course removed the ID property from each class. So the two entities are identical except the ID property is now implemented in the base class.

After compiling and running my project, I get the following "famous" EF runtime error:

"Sequence contains more than one matching element"

I know that this error is related to the many-to-many association, because if I remove the many-to-many mapping from the Process class, everything runs correctly, but without the association of course.

Can anyone see what the problem is here? Is this a CTP5 bug or is there something wrong in my i开发者_开发百科mplementation? If it turns out to be a bug, do you have a suggestion for a workaround?


I ran into the same problem, only with a many-to-one association. See this answer

In a nutshell: don't use a base entity class until this is fixed; you can use an interface instead for the Id property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜