简体   繁体   English

实体框架(CF)多个表PK到同一FK

[英]Entity Framework(CF) multiple tables PK to same FK

So I got a few tables like this: 所以我得到了一些这样的表:

    public class Family
{
    public Guid Id { get; set; }
    public virtual Career Career { get; set; }
}

public class Career
{
    public Guid Id { get; set; }
}

And a few more classes like that. 还有更多这样的课程。

And then a table where all translations are for them depending on language. 然后是一个表格,其中所有翻译都取决于语言。

public class Language
{
    public string LanguageCulture { get; set; }
    public Guid ObjectId { get; set; }
    public string Translation { get; set; }
}

My issue here is that I want Language to understand that ObjectId can come from any of these classes. 我在这里的问题是我希望Language了解ObjectId可以来自这些类中的任何一个。 How can I accomplish that? 我该怎么做? By default is creates a column for each table that refrences to it. 默认情况下,为为其引用的每个表创建一列。

If you want to be able to use all the niceness of LINQ/Lambda (and for the sake of your relational model) I would suggest that you dont go down this path. 如果您想使用LINQ / Lambda的所有优点(并且为了您的关系模型),我建议您不要走这条路。

I would either create a separate link from Language to each class, or link directly to LanguageCulture and Translation from the classes that need it. 我要么创建从Language到每个类的单独链接,要么直接从需要类的类中直接链接到LanguageCultureTranslation

I would want to achieve a model which makes it as intuitive as possible to retrieve and work with the data, even though this may be a bit of a annoyance now when building it. 我想建立一个模型,使其尽可能直观地检索和处理数据,即使现在在构建数据时可能有点烦人。 I'm quite confident that you will be happy that you created real relationships a few months / years down the line. 我非常有信心您会很高兴在几个月/几年后建立真正的关系。

If you were to continue with your example you would have something like: 如果要继续执行示例,您将遇到类似以下内容的情况:

var object = _dataset.Where( x=> x.somecolumn == somevalue);
var language = _dataset.Where( x => x.ObjectType == object.Type
                                    && x.ObjectID == object.ID );

Instead of simply doing 而不是简单地做

var object = _dataset.Where( x=> x.somecolumn == somevalue);
object.Language //if you create a separate link to each object
object.LanguageCulture // if you remove your Language object and link directly to what it is your after

In the latter you just need to get your original object and you could also make subqueries such as 在后者中,您只需要获取原始对象,还可以进行子查询,例如

var objects = _dataset.Where( x=> x.somecolumn == somevalue
                                 && x.Language.LanguageCulture.Country == "Sweden")

This query is still possible with joins in your original idea but in general will be more annoying to do. 使用您的原始想法进行联接时,仍然可以进行此查询,但总的来说,这样做会比较麻烦。

If you want to avoid writing the same thing over and over you could use an abstract class with the language fields that you can inherit in all the classes that need it. 如果您想避免一遍又一遍地写同一件事,则可以将抽象类与可以在需要它的所有类中继承的语言字段一起使用。

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

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