2016-10-13 5 views
1

나는 서버에서 텍스트를 가져 와서 데이터베이스에 삽입 한 다음 사용자 기본 언어를 기반으로 적절한 텍스트를 찾는 다중 언어 응용 프로그램을위한 큰 데이터베이스를 보유하고 있습니다. enter image description here 그림 :엔티티 프레임 워크의 매핑 예외

나를 먼저 데이터베이스를 설명 내 문제를 다음 내가 말하는거야 보자 예를 들어 내가 다시 TranslationEntry에 연결 Translation 테이블에 대한 외래 키 (Description 열)이있는 테이블 Product을 가지고 모든 언어로 제품 설명이 번역 된 테이블

TranslationEntry 테이블에 외래 키가있는 Language이라는 별도의 테이블에 언어가 있습니다.

public class Product : BaseModel 
{ 
    public int description { get; set; } 
    public virtual Translation Description { get; set; } 
} 

public class Translation : BaseModel 
{ 
    public Translation() 
    { 
     Products = new List<Product>(); 
    } 
    public virtual ICollection<Product> Products { get; set; } 
    public virtual ICollection<MainCategory> MainCategories { get; set; } 
    public virtual ICollection<Caption> Captions { get; set; } 
} 

public class TranslationEntry : BaseModel 
{ 
    public string text { get; set; } 
    public int language { get; set; } 
    public virtual Language Language { get; set; } 
    public int translation { get; set; } 
    public virtual Translation Translation { get; set; } 
} 

public class Language : BaseModel 
{ 
    public Language() 
    { 
     TranslationEntries = new List<TranslationEntry>(); 
    } 

    public string title { get; set; } 
    public string language_code { get; set; } 
    public virtual ICollection<TranslationEntry> TranslationEntries { get; set; } 
} 

public class BaseModel 
{ 
    public int id { get; set; } 
    public int MembershipId { get; set; } 
    public SyncStatus SyncState { get; set; } 
    .... 
} 

번역 항목 매핑 :

 HasRequired(translationEntry => translationEntry.Translation) 
      .WithMany(translation => translation.TranslationEntries) 
      .HasForeignKey(translationEntry => 
       new {translationEntry.translation, translationEntry.MembershipId, translationEntry.SyncState}) 
       .WillCascadeOnDelete(false); 

     HasRequired(translationEntry => translationEntry.Language) 
      .WithMany(language => language.TranslationEntries) 
      .HasForeignKey(translationEntry => 
       new {translationEntry.language, translationEntry.MembershipId, translationEntry.SyncState}) 
       .WillCascadeOnDelete(false); 

     Property(t => t.translation) 
      .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_translatinlanguageOd", 1) { IsUnique = true })); 
     Property(t => t.language) 
      .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_translatinlanguageOd", 2) { IsUnique = true })); 

제품 매핑 : 여기에 데이터의

HasRequired(product => product.Description) 
      .WithMany(translation => translation.Products) 
      .HasForeignKey(product => new { product.description, product.MembershipId, product.SyncState }) 
      .WillCascadeOnDelete(false); 

샘플 세트 :

enter image description here

이제 홍보 oblem : 번역 같은 관계 등이 많은 단체가 있다는 것을

A 'Mapping' exception occurred while processing the query. See the inner exception.

Inner exception:
More than one property map found for property 'translation' when using case-insensitive search.

참고 : 나는 다음 명령

var o = databaseContext.Products.ToList().First(p=>p.id==1)?.Description.TranslationEntries.First(te=>te.language==1); 

를 사용하지만 오류가 발생, 제품의 설명을 얻으려면 내가 설명한 테이블 Product.


UPDATE : 내 임시 해결책은 이것이다 :

var Language = context.Languages.Include(l => l.TranslationEntries) 
         .Where(l => l.id == languageId) 
         .ToList() 
         .FirstOrDefault(); 
TranslationEntries = Language?.TranslationEntries; 
var translatedText = (from t in TranslationEntries where t.translation == 2 select t.text).FirstOrDefault(); 

답변

0

마지막으로이 바보 같은 문제를 해결! 오류 메시지가 말하듯이 문제는 대소 문자를 구분하지 않는 검색이므로 TranslationEntry 클래스에 번역이라는 2 개의 속성이 있습니다. 그 중 하나의 이름이 변경되었으므로 이제는 아무 문제없이 작동합니다!

관련 문제