简体   繁体   English

具有Entity Framework Core的可翻译模型

[英]Translatable models with Entity Framework Core

I am migrating my php application to a .net core application using Entity Framework Core. 我正在使用Entity Framework Core将php应用程序迁移到.net核心应用程序。

Several of my models have translations linked to them from the translations table. 我的几个模型都有与translations表关联的translations The way the models are linked to the translations is by modelname and modelid . 将模型链接到翻译的方式是通过modelnamemodelid The modelname is the name of the class and the id is the id of that object. modelname是类的名称,而id是该对象的ID。 Keep in mind, an object can have multiple translations with a unique slug. 请记住,一个对象可以有多个翻译,并且翻译时要带有唯一的子弹。
I was wondering if it is possible to easily implement this using entity framework. 我想知道是否可以使用实体框架轻松实现此目的。 So the models are linked by their id, but also filtered on their own class name. 因此,模型通过其ID进行链接,但也根据其自己的类名进行过滤。 Will I have to create this relation in fluent api for every model or is there a better solution for this? 我是否必须为每个模型都在流利的api中创建这种关系,还是有更好的解决方案?

Below is a small example of how the database looks like. 下面是一个有关数据库外观的小示例。 But in the real database, there are many more tables that have translations. 但是在实际的数据库中,还有更多具有转换的表。 The database can, of course, be modified if needed. 当然,可以根据需要修改数据库。

在此处输入图片说明

It seems that you want to achieve polymorphic association with TPC strategy. 看来您想与TPC策略实现多态关联。

Unfortunately current version of EF Core 2.0 does not support TPC and feature is planned for release in EF Core 2.1 - https://github.com/aspnet/EntityFrameworkCore/wiki/Roadmap 不幸的是,当前版本的EF Core 2.0不支持TPC,并且该功能计划在EF Core 2.1中发布-https: //github.com/aspnet/EntityFrameworkCore/wiki/Roadmap

This is the solution that works with EF 6.0: 这是适用于EF 6.0的解决方案:

public class Context : DbContext
{

    public Context() : base()
    {

    }

    public IDbSet<Translation> Translations { get; set; }

    public IDbSet<TranslatedModel> TranslationModels { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Store>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("Stores");
            });

        modelBuilder.Entity<Publisher>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("Publishers");
            });

        modelBuilder.Entity<Translation>()
            .HasRequired(t => t.TranslatedModel)
            .WithRequiredDependent(t => t.Translation);
    }
}

public class Translation
{
    public int Id { get; set; }

    public string Value { get; set; }

    public TranslatedModel TranslatedModel { get; set; }
}

public abstract class TranslatedModel
{
    public int Id { get; set; }

    public Translation Translation { get; set; }
}

public class Store : TranslatedModel
{
    public int Website { get; set; }

}

public class Publisher : TranslatedModel
{
    public string Website { get; set; }
}

Problem here is that you may have the same ids for Products and Stores, resulting in clashes. 这里的问题是,您可能对商品和商店具有相同的ID,从而导致冲突。 So I would recommend instead of having model_id and model_name as reference key, just change your ids type from int to GUID and drop the model_name. 因此,我建议不要将model_id和model_name用作参考键,而只需将您的id类型从int更改为GUID并删除model_name。

more info here: 更多信息在这里:

https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-指导方针

What I know that EF DOES offer that might help is that EF offers polymorphism based off of conditions. 我知道EF确实提供的帮助可能是EF提供了基于条件的多态性。 You could implement a Model class using "Translations" as the table base, have Products/Stores/Publishers inherit from Model, and define the condition upon which a Product/Store/Publisher is instantiated be based off the "model_name" field. 您可以使用“转换”作为表基础来实现Model类,让Products / Stores / Publisher从Model继承,并根据“ model_name”字段定义实例化Product / Store / Publisher的条件。

I'm not entirely sure, but in the child classes, you may be able to define those navigator properties as only relevant to one type. 我不确定,但是在子类中,您可以将那些导航器属性定义为仅与一种类型相关。

Here is the guide: 这是指南:

https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application https://docs.microsoft.com/zh-cn/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-一个asp-net-mvc应用程序

The solution I used to solve this is adding a new table, Translatables . 我用来解决此问题的解决方案是添加一个新表Translatables every table that is translatable now has a field translatable_id and all translations are linked to a Translatable . 现在,每个可翻译表都具有一个字段translatable_id并且所有翻译都链接到Translatable

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

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