Hesam Kashefi Hesam Kashefi - 1 month ago 11
C# Question

Mapping exception in Entity Framework

I have a big database for a multi lingual application that gets it's texts from the server , inserts into the database, then based on user preferred language, finds appropriate text.

Let me first describe the database then I'll say my problem:
enter image description here
Illustration: for example I have a table

Product
, which has a foreign key (
Description
column) to the
Translation
table which in turn connects to
TranslationEntry
table that has all the translations of products's description in all languages.

The languages are in a separate table called
Language
which has a foreign key to
TranslationEntry
table.

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


Translation Entry Mapping:

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


Product Mapping:

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


Sample set of data here:

enter image description here

Now the problem: I want to get description of a product, I use the following command

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


but I get an error:


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.


Note that there are many entities which have the same relationships for translation as
Product
table which I showed.




UPDATE
:
my temporary Solution is this:

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();

Answer

Finally fixed this stupid problem! As the error message says, the problem is by case-insensitive search there are 2 property named translation in TranslationEntry Class, I renamed one of them and now everything works without any problem!

Comments