简体   繁体   English

数据库如何表示一对一关系?

[英]How does the database represent One-to-One relationship?

I am using a simple One-to-One relationship in entity framework core 2.0. 我在实体框架核心2.0中使用了简单的一对一关系。

   modelBuilder.Entity<Profile>()
        .HasOne(p => p.Avatar)
        .WithOne()
        .HasForeignKey<Profile>(p => p.AvatarId);

Where every profile has only one avatar. 每个配置文件只有一个头像。

Looking in database managing apps does not show me (or at least I did not find) any differences between One-to-One and One-to-Many relationships. 在数据库管理应用程序中查看并不会显示(或至少没有找到)一对一和一对多关系之间的任何差异。 So what I mean I could not find any special constrains for One-to-One different that One-to-Many. 所以我的意思是我找不到一对多不同于一对多的任何特殊约束。

How does the database know which kind of relationships I defined? 数据库如何知道我定义的哪种关系? If it does not know, why would I then define the type of relationship I am using? 如果不知道,那为什么还要定义我正在使用的关系类型?

If you can't see any foreign keys or primary keys in your database then it doesn't know. 如果您在数据库中看不到任何外键或主键,则不会知道。 Or more specifically: it you will be allowed to save data which violates the relationship. 或更具体地说:允许您保存违反关系的数据。

Integrity of relationships are enforced in databases with Foreign Keys but this certainly does not stop you submitting SQL which joins tables incorrectly. 关系的完整性在具有外键的数据库中得到了强制执行,但这当然不会阻止您提交错误地联接表的SQL。

Here's a link to defining foreign keys that ensures that a one to one relationship is guaranteed based on the data in tables. 这是定义外键的链接,可确保基于表中的数据确保一对一关系。 http://www.tech-recipes.com/rx/56738/one-to-one-one-to-many-table-relationships-in-sql-server/ . http://www.tech-recipes.com/rx/56738/one-to-one-one-to-many-table-relationships-in-sql-server/

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

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