Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Composite key fluent API

I am trying to map a composite key for an entity.

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

And its Map:

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

An Image:

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

And its map:

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

And the navigation property:

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

And 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 keep getting 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.

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

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

And its map:

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

I've tried many variations of the definitions but none seems to work. Any idea? Is it EF bug?

like image 986
Tomer Avatar asked Oct 16 '13 08:10

Tomer


1 Answers

As it turned out, I simply forgot putting the map on the DbContext:

modelBuilder.Configurations.Add(new CustomerImageMap());

That said, the composite Id still not being populated on the $metadata this way. And so using data annotaion this is the metadata that is generated:

<EntityType Name="CustomerImage">
    <Key>
        <PropertyRef Name="CustomerId"/>
        <PropertyRef Name="ImageId"/>
    </Key>
    <Property Name="CustomerId" Type="Edm.Int32" Nullable="false"/>
    <Property Name="ImageId" Type="Edm.Int32" Nullable="false"/>
    <Property Name="LastUpdated" Type="Edm.DateTime"/>
    <NavigationProperty Name="Customer" Relationship="EasyBizy.Entities.Models.EasyBizy_Entities_Models_CustomerImage_Customer_EasyBizy_Entities_Models_Customer_CustomerPartner" ToRole="Customer" FromRole="CustomerPartner"/>
    <NavigationProperty Name="Image" Relationship="EasyBizy.Entities.Models.EasyBizy_Entities_Models_CustomerImage_Image_EasyBizy_Entities_Models_Image_ImagePartner" ToRole="Image" FromRole="ImagePartner"/>
</EntityType>

However, if using fluent API instead of data annotation, the key part is not being generated at all. Why?

like image 129
Tomer Avatar answered Oct 30 '22 21:10

Tomer