2012-03-28 13 views
1

In meiner Klasse produc habe ich eine Sammlung von Fotos. Eines der Fotos in dieser Sammlung stellt das Hauptproduktfoto dar.Objektnavigation Referenz in EF

Meine Klasse von Produkten

public class Product 
{ 
    public Guid ID { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public virtual ICollection<Photo> Photos { get; set; } 
    public virtual Photo Photo { get; set; } 
} 

Die Eigenschaft photo muss ein Foto in der Fotosammlung zeigen.

ProducsConfiguration

das Schema der Datenbank zu erstellen, verwenden Sie die folgende Konfiguration:

public class ProductConfiguration : EntityTypeConfiguration<Product> 
{ 
    public ProductConfiguration() 
    { 
     HasKey(p => p.ID) 
      .Property(p => p.ID) 
      .IsRequired(); 

     Property(p => p.Name) 
      .IsRequired() 
      .HasMaxLength(65); 

     HasMany(p => p.Photos).WithMany().Map(m => m.ToTable("ProductPhotos")); 
     ...???... 
    } 
} 

Frage

Wie würde die Konfiguration (FLUENT Konfiguration EF verwenden) für in Bezug auf die Eigenschaft photos als Verweis auf eines der Fotos in der Sammlung Photos ??

Danke!

Antwort

0

Angenommen, das war ein Tippfehler und Sie beziehen sich auf die Photo Eigenschaft, es gibt nichts, was Sie tun müssen.

Standardmäßig wird Photo eine Referenz auf ein Foto sein. Technisch gesehen hat das Foto keine, um Teil der Photos Sammlung zu sein (und es gibt keine Möglichkeit, EF dazu zu bringen, dies zu validieren), aber es wird das erreichen, was Sie brauchen (mit einem "Haupt" -Foto)