Question

For an entity, I'm attempting to map a composite key.

public class Customer 
{
    public int CustomerId { get; set; }
    public virtual List<CustomerImage> CustomerImages { get; set; }
}

Also, its Map

public class CustomerMap : EntityTypeConfiguration<Customer>
{
    public CustomerMap()
    {
        HasKey(t => t.CustomerId);
        ToTable(DbConstants.k_CustomersImageTable);
    }
}

a Picture

public class Image
{
    public int ImageId { get; set; }
}

Also, its map

public class ImageMap : EntityTypeConfiguration<Image>
{
    public ImageMap()
    {
        HasKey(t => t.ImageId);
        ToTable(DbConstants.k_ImagesTable);
    }
}

Moreover, the navigational feature

public class CustomerImage
{
    public int CustomerId { get; set; }
    public int ImageId { get; set; }
    public virtual Customer CustomerRelated { get; set; }
    public virtual Image ImageRelated { get; set; }
}

Also, its map

public class CustomerImageMap : EntityTypeConfiguration<CustomerImage>
{
    public CustomerImageMap()
    {
        HasKey(t => new { t.CustomerId, t.ImageId });
        Property(t => t.CustomerId).IsRequired().HasColumnOrder(0);
        Property(t => t.ImageId).IsRequired().HasColumnOrder(1);
        HasRequired(t => t.ImageRelated).WithMany().HasForeignKey(x => x.ImageId);
        HasRequired(p => p.CustomerRelated)
            .WithMany(p => p.CustomerImages)
            .WillCascadeOnDelete(false);

        ToTable(DbConstants.k_CustomersImageTable);
    }
}

But I continue to encounter the following exception:

One or more validation errors were detected during model generation:
System.Data.Entity.Edm.EdmEntityType: : EntityType 'CustomerImage' has no key defined. Define the key for this EntityType.
System.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'CustomerImages' is based on type 'CustomerImage' that has no keys defined.

It works wonderfully if I define the composite key with data annotations, which is not very nice: However

public class CustomerImage
{
    [Key, Column(Order = 0)]
    public int CustomerId { get; set; }
    [Key, Column(Order = 1)]  
    public int ImageId { get; set; }
}

Also, its map

public class CustomerImageMap : EntityTypeConfiguration<CustomerImage>
{
    public CustomerImageMap()
    {
        ToTable(DbConstants.k_CustomersImageTable);
    }
}

I've tried several other definitions, but none of them seem to be effective. Any ideas? Is this EF bug?

1
5
7/22/2014 9:53:14 PM

Accepted Answer

ZZZ_tmp
10
7/22/2014 9:50:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow