1

업데이트 : 좀 더 연구를 마친 후 내 다 대다 매핑의 숫자이 작동하지 않습니다. 음 ...many to many 매핑이 작동하지 않습니다 - EF 4.1 RC

EF 4.1 CTP4에서 EF 4.1 RC로 데이터 액세스 프로젝트를 업그레이드 중이며 새로운 EntityTypeConfiguration<T> 설정에 문제가 있습니다.

특히 다 대다 관계에 문제가 있습니다. .First() 항목을 가져올 때 Sequence contains no elements 예외가 발생합니다.

특별한 예외는 실제로 그렇게 흥미롭지 않습니다. 그것이 말하는 것은 아이템이 없다는 것입니다. BUT 컬렉션에 아이템이 있어야한다는 것을 알고 있습니다. 따라서 새로운 맵핑에 문제가있을 것입니다.

제품 모델

public class Product : DbTable 
{ 
    //Blah 

    public virtual ICollection<Tag> Categories { get; set; } 

    public Product() 
    { 
     //Blah 
     Categories = new List<Tag>(); 
    } 
} 

BaseConfiguration

public class BaseConfiguration<T> : EntityTypeConfiguration<T> where T : DbTable 
{ 
    public BaseConfiguration() 
    { 
     this.HasKey(x => x.Id); 
     this.Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
     this.Property(x => x.UpdatedOn); 
     this.Property(x => x.CreatedOn); 
    } 
} 

ProductConfiguration

0 : 여기에

내가 지금까지 가지고있는 코드입니다
public class ProductConfiguration : BaseConfiguration<Product> 
{ 
    public ProductConfiguration() 
    { 
     this.ToTable("Product"); 

     //Blah 

     this.HasMany(x => x.Categories) 
      .WithMany() 
      .Map(m => 
      { 
       m.MapLeftKey("Tag_Id"); 
       m.MapRightKey("Product_Id"); 
       m.ToTable("ProductCategory"); 
      }); 
    } 
} 

이전 CTP4 매핑 된 작업!

this.HasMany(x => x.Categories) 
    .WithMany() 
    .Map("ProductCategory", (p, c) => new { Product_Id = p.Id, Tag_Id = c.Id }); 

누구든지 고칠 필요가있는 것을 볼 수 있습니까? 더 많은 코드를 제공하기를 원한다면 알려주십시오.

편집 : 더 코드

DBTABLE

public class DbTable : IDbTable 
{ 
    public int Id { get; set; } 
    public DateTime UpdatedOn { get; set; } 
    public DateTime CreatedOn { get; set; } 
} 

태그

public class Tag 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Slug { get; set; } 
    public bool Visible { get; set; } 
    public virtual TagType TagType { get; set; } 
} 

TagConfiguration

public class TagConfiguration : EntityTypeConfiguration<Tag> 
{ 
    public TagConfiguration() 
    { 
     this.ToTable("Tags"); 

     this.HasKey(x => x.Id); 
     this.Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName("tag_id"); 
     this.Property(x => x.Name).HasMaxLength(300).HasColumnName("tag_name"); 
     this.Property(x => x.Slug).HasMaxLength(500).HasColumnName("tag_slug"); 
     this.Property(x => x.Visible).HasColumnName("tag_visible"); 
     this.HasRequired(x => x.TagType).WithMany(tt => tt.Tags).Map(m => m.MapKey("tagtype_id")); 
    } 
} 

예,이 이름 지정 규칙은 up to boohai 인 레거시 데이터베이스입니다.

제품에 Tag으로 매핑되고 올바르게로드되는 다른 속성 인 Specialization이 있기 때문에 Tag 클래스를 올바르게 연결해야한다는 것을 알고 있습니다. 그러나 일대 다 방식으로 매핑된다는 점에 유의하십시오. 그래서 Tag으로 many-to-many 인 것처럼 보입니다.

다 대 다 연관이 작동하는지 확인해 보겠습니다.

+0

this.HasMany(x => x.Categories) .WithMany(category=>category.Products) .Map(m => { m.MapLeftKey(t => t.TagId, "Tag_Id"); m.MapRightKey(t => t.ProductId, "Product_Id"); m.ToTable("ProductCategory"); }); 

(교차 손가락 ...) : 제품에 다시 가리키는 WithMany 속성에 람다를 추가

시도 나를 위해. –

+0

Niggly! 내 코드를 시험해 보는 것에 대해 매우 감사하지만, 내가 듣고 싶은 것은 아닙니다! 내가 문제가 될 수 있다고 생각했기 때문에 나는 MARS를 켰다. 어디에서 볼 수 있겠습니까? – Charlino

+0

말하기 어렵습니다. 문제는 보이지 않기 때문에 다른 코드 스 니펫을 보여줘야합니다. –

답변

1

many to many 매핑을 수행하려면 두 검색 결과 모두 지정해야합니다. 난 그냥 코드를 사용하고 작동하기 때문에 문제는 다른 곳에서해야합니다

+0

감사합니다.이 도움이되었습니다. – Omu

0

코드 우선 접근 방식을 아직 사용하지 않았지만 POCO로 작업 할 때 지연 등록 기능을 사용 설정해야 탐색 속성이 작동합니다. 이것은 물론 의도적으로 설계되었지만, Code-First에 대해이 동작을 명시 적으로 활성화해야하는지 여부는 알 수 없습니다.

+0

그리고 어떻게해야합니까? #lazyweb – Charlino

+0

그래, 기본적으로 활성화되어 있습니다. 명시 적으로 true로 설정해도 도움이되지 않습니다 .-( – Charlino

+0

EF4.1 dbcontext에서 기본적으로 지연로드가 활성화됩니다. :) –