简体   繁体   English

实体框架中的映射异常

[英]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: 让我先描述数据库,然后再说我的问题: 在此处输入图片说明 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. 插图:例如,我有一个表Product ,该表具有Translation表的外键( Description列),该表又连接到TranslationEntry表,该表具有所有语言的产品说明的所有翻译。

The languages are in a separate table called Language which has a foreign key to TranslationEntry table. 这些语言位于名为Language的单独表中,该表具有TranslationEntry表的外键。

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: 此处的数据样本集:

在此处输入图片说明

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. 请注意,有许多实体与我显示的“ Product表具有相同的翻译关系。


UPDATE : my temporary Solution is this: 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();

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! 如错误消息所述,问题是不区分大小写的搜索,在TranslationEntry类中有2个名为translation的属性,我重命名了其中一个属性,现在一切正常,没有任何问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM