简体   繁体   English

如何识别一对一关系中的依赖实体 Entity Framework Core?

[英]How to identify a dependent entity in one to one relationship Entity Framework Core?

I have studied many articles about how to config one to one relationships.我研究了很多关于如何配置一对一关系的文章。 I have learned it.我已经学会了。

But I couldn't understand how to find a dependent entity in one to one relationship?但是我不明白如何在一对一的关系中找到一个依赖实体?

For instance, we have two entities User and Developer .例如,我们有两个实体UserDeveloper How to understand which of them is a dependent entity?如何理解它们中的哪一个是依赖实体? Because I think we must add a foreign key to a dependent entity.因为我认为我们必须为依赖实体添加外键。 So the first things to do, we need to find a dependent entity.所以要做的第一件事,我们需要找到一个依赖实体。

public class User
{
    public int Id {get;set;}
    public string PasswordHash { get; set; }
    public string FullName { get { return FirstName + " " +LastName;}}
    public GenderType Gender { get; set; }
    public bool IsActive { get; set; }
    public DateTimeOffset LastLoginDate { get; set; }
    public string FirstName { get ; set ; }
    public string LastName { get; set; }
}

public class Developer
{
    public int Id { get; set; }
    public byte Image { get; set; }
    public int Age { get; set; }
    public string Resume { get; set; }
}

A dependent is something that depends on something else.依赖是依赖于其他东西的东西。 A baby is dependent on its mother for food etc. Identify which entity can stand alone, without the other.婴儿依赖其母亲提供食物等。确定哪个实体可以独立存在,而没有另一个实体。

For example, you may decide that a User might not be a Developer but a Developer is always a User - in this case your relationship is actually 1:0..1 (user:developer) and you're looking at Developer being a subclass of a User.例如,您可能决定用户可能不是开发人员,但开发人员始终是用户 - 在这种情况下,您的关系实际上是 1:0..1 (user:developer) 并且您将 Developer 视为子类用户的。 If.如果。 You could alternatively arrange things in a has-a fashion, and Developer has a User property (but User doesn't have a Developer property because not every User is a developer)您也可以以一种方式安排事情,并且 Developer 有一个 User 属性(但 User 没有 Developer 属性,因为并非每个 User 都是开发者)

You may decide that you can never have one without the other- in which case they would probably be a good candidate for being in the same table/same client side entity您可能会决定,如果没有另一个,您永远无法拥有一个 - 在这种情况下,它们可能是位于同一个表/同一个客户端实体中的好人选

To some extent the question can be academic;在某种程度上,这个问题可能是学术性的; there may be situations where you want to treat one as dependent, and others where it's the inverse way round.在某些情况下,您可能希望将一个人视为依赖者,而在其他情况下则相反。 It'll probably help you overall though if you make a decision about how your entities are related on the client side and this will drive how you map them on the database side如果您决定实体在客户端的关联方式,它可能会总体上对您有所帮助,这将推动您在数据库端映射它们的方式

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

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