简体   繁体   中英

How does the database represent One-to-One relationship?

I am using a simple One-to-One relationship in entity framework core 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.

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/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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