2012-11-29 188 views
2

我已经有三个表的数据库结构如下:流利的API映射

Table 1: Product 
Columns: ProductID, PK 
     SKU,  PK 
     Name 

Table 2: ProductImages 
Columns: ImageID PK 
     SKU 

Table 3: Images 
Columns: ImageID PK 
     ImageContent 

忽略了一会儿该表ProductImages看起来是不同之处在于独特的PK约束是许多一对多关系强制为一对一,因此是不必要的一对多表(这是一个现有的数据库)。

我想有以下POCO实体类:

public class Product 
{ 
    public int ProductId { get; set; } 
    public string SKU { get; set; } 
    public virtual Image Image { get; set; } 
} 

假设Image也是我的实体模型的实体。

我第一次使用Entity Framework 5.0与代码和Fluent API,我要疯了试图找出如何写ProductMap类(从EntityTypeConfiguration<Product>派生)。具体来说就是关系映射。

渲染的SQL应该像实体框架的版本如下:

select p.SKU, p.Name, p.ProductID, I.ImageID, I.ImageContent 
from Products p 
inner join ProductImages si on p.SKU = si.SKU 
inner join Images i on i.ImageId = si.ImageId 

任何帮助,任何人都可以提供将与心脏得到满足感到赞赏。

+0

SKU不应该是ProductImages的主键吗?如果是这种情况,那么您会希望收集产品图像集合,该图像集合包含在您的产品类中。 – jfin3204

+0

你不打算这样做,除非你愿意通过告诉它产品只有SKU作为主键来欺骗EF。 (多对多映射是可能的)。但这可能会导致异常,因为实际上SKU在产品中可能不是唯一的。 –

回答

3

我有同样的问题,直到我开始使用Entity Framework Power Tools

使用它可以生成清晰的实体,如一个业务对象和映射类。 好文章,帮助我创造出惊人的数据访问层:Reverse Engineer Code First

,我认为应该映射看起来像:

public class ProductMap : EntityTypeConfiguration<Product> 
{ 
    public ProductMap() 
    { 
     // Primary Key 
     this.HasKey(t => t.ProductId); 

     // Properties 
     this.Property(t => t.Name) 
      .IsRequired() 
      .HasMaxLength(256); 

     // Table & Column Mappings 
     this.ToTable("Product"); 
     this.Property(t => t.ProductId).HasColumnName("ProductID"); 
     this.Property(t => t.Name).HasColumnName("Name"); 

     // Relationships 
     this.HasMany(t => t.Products) 
      .WithMany(t => t.Images) 
      .Map(m => 
      { 
       m.ToTable("ProductImages"); 
       m.MapLeftKey("ProductID"); 
       m.MapRightKey("ImageID"); 
      }); 
     } 
    } 
+0

谢谢你的回复。我已经使用这个工具。这两个问题是Products.SKU - > ProductImages。SKU在数据库中不存在(因此不能被反向工程化),第二个问题是即使它可以,映射的POCO也将是Products.ProductImages [0] .Image。我需要将这种关系展平为Products.Image。 –

0

我想通了。数据库结构和我的实体模型之间存在逻辑上的分离。

productImages表支持* -1映射,因此产品POCO类需要有一个ProductImages集合。然后,需要配置实体如下:

public class ProductImagesMap : EntityTypeConfiguration<ProductImage> 
{ 
     //Other field and key configuration... 

     this.HasRequired(t=>t.Image).WithRequiredDependent(); 

     this.HasRequired(t => t.Product) 
     .WithMany(t => t.ProductImage) 
     .HasForeignKey(d => d.SKU); 
} 

public class ProductImage 
{ 
    public int ImageId { get; set; } 
    public string SKU{ get; set; } 
    public virtual Image Image { get; set; } 
    public virtual Product Product { get; set; } 
} 

public class Product 
{ 
    public Product() 
    { 
     this.Features = new List<Feature>(); 
    } 

    public int ProductId { get; set; } 
    public string Name { get; set; } 
    public string SKU{ get; set; } 
    public virtual Brand Brand { get; set; } 
    public virtual ICollection<ProductImage> ProductImages { get; set; } 
} 

我尝试配置在ProductMap类这种关系(如产品在父/母校表)挣扎着,但直到从ProductImages类配置它它的工作,所以我的两个开放的问题是:

1)什么规则确定哪个实体驱动关系配置?

2)有没有一种方法可以将产品POCO类配置为具有ICollection Images属性并绕过ProductImage实体,因为ProductImage仅用作​​产品和图像之间的链接表?