Entity Framework 6 Code First Foreign Key Without Corresponding Properties

Stilgar

I have the tables that look like this

Users
 - UserID (PK)
 - UserName
 - ...

UserSettings
 - UserID (PK)
 - Settings

My UserSetting entity looks like this

public class UserSetting
{
    public UserSetting()
    {
    }

    [Key]
    [Column("UserID", TypeName ="nvarchar(128)")]
    public string ID { get; set; }

    [Required]
    public string Settings { get; set; }
}

I want the PK of UserSettings to add a FK from UserID in Users to UserID in UserSettings as the relationship is supposed to be 1:1 (in case you are wondering the settings field is a string that the frontend just dumps in the database as a json string and the backend does not care about the values inside)

This works fine if I create the database by hand and add the foreign key and EF knows nothing about it but I decided to try letting EF create the DB for this project. Is there a way to tell EF to create that FK without adding entity properties for the relation to either of the entities?

octavioccl

To create a relationship in Code First you need to have at least a navigation property in one of both ends (this kind of relationship are called unidirectional relationships). A typical one to one relationship is configured as follow:

public class User
{
    public User()
    {
    }

    [Key]
    [Column("ID", TypeName ="nvarchar(128)")]
    public string ID { get; set; }

    public string Name{ get; set; }

    public virtual UserSetting UserSetting {get;set;} 
}

public class UserSetting
{
    public UserSetting()
    {
    }

    [Key,ForeignKey("User"),Column("UserID")]
    public string ID { get; set; }

    //...

    public virtual User User{get;set;}
}

Now, with this model you aren't adding any new column to your tables, it's going to be the same, only now you're configuring the PK of UserSettings table as FK of the relationship (what you are trying to achieve).

Now, as I said EF needs at least one navigation property to create the relationship. So, if you want, you can delete the UserSetting nav. property on User, that's ok, leaving the data annotations over the Id of UserSetting and the User navigation property is enough to Code First to create the relationship.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Foreign Key in Code First Entity Framework

From Dev

Entity Framework: Foreign Key in code first

From Dev

How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

From Dev

Entity Framework 6 multiple table to one foreign key relationship code first

From Dev

Entity framework 6 code first saving collection via foreign key relation

From Dev

Defining multiple Foreign Key for the Same table in Entity Framework Code First

From Dev

Entity Framework Code First Foreign Key adding Index as well

From Dev

Entity Framework Code First and Firebird - Foreign Key name issue

From Dev

Entity Framework Code first adds unwanted foreign key column

From Dev

Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

From Dev

Multiple Foreign Key for Same table in Entity Framework Code First

From Dev

Get foreign key value using Entity Framework code first

From Dev

Entity Framework one to optional foreign key code first fluent mapping

From Dev

Entity framework code first cant create primary and foreign key relationship

From Dev

Entity framework code first, get access to foreign key value

From Dev

Entity framework code first, custom foreign key name for inheritance

From Dev

How to specify a foreign key with code-first Entity Framework

From Dev

Exchanged Foreign Key on Entity Framework Code First From data base

From Dev

Entity framework code first using navigation properties a Key

From Dev

Entity Framework 6 code first: setting unicode to false for string properties

From Dev

Entity Framework 6 code first: setting unicode to false for string properties

From Dev

Code First Entity Framework 6: 1 to 1 with composite key

From Dev

Entity Framework 6 Adding foreign key

From Dev

Unable to update Foreign Key in Entity Framework 6

From Dev

Entity Framework 6 - foreign key issue

From Dev

Entity Framework 6 could not create a foreign key

From Dev

Entity Framework 6 Foreign Key Issue

Related Related

  1. 1

    Entity Framework Code First Foreign Key issue

  2. 2

    Entity Framework Code First Foreign Key issue

  3. 3

    Foreign Key in Code First Entity Framework

  4. 4

    Entity Framework: Foreign Key in code first

  5. 5

    How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

  6. 6

    Entity Framework 6 multiple table to one foreign key relationship code first

  7. 7

    Entity framework 6 code first saving collection via foreign key relation

  8. 8

    Defining multiple Foreign Key for the Same table in Entity Framework Code First

  9. 9

    Entity Framework Code First Foreign Key adding Index as well

  10. 10

    Entity Framework Code First and Firebird - Foreign Key name issue

  11. 11

    Entity Framework Code first adds unwanted foreign key column

  12. 12

    Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

  13. 13

    Multiple Foreign Key for Same table in Entity Framework Code First

  14. 14

    Get foreign key value using Entity Framework code first

  15. 15

    Entity Framework one to optional foreign key code first fluent mapping

  16. 16

    Entity framework code first cant create primary and foreign key relationship

  17. 17

    Entity framework code first, get access to foreign key value

  18. 18

    Entity framework code first, custom foreign key name for inheritance

  19. 19

    How to specify a foreign key with code-first Entity Framework

  20. 20

    Exchanged Foreign Key on Entity Framework Code First From data base

  21. 21

    Entity framework code first using navigation properties a Key

  22. 22

    Entity Framework 6 code first: setting unicode to false for string properties

  23. 23

    Entity Framework 6 code first: setting unicode to false for string properties

  24. 24

    Code First Entity Framework 6: 1 to 1 with composite key

  25. 25

    Entity Framework 6 Adding foreign key

  26. 26

    Unable to update Foreign Key in Entity Framework 6

  27. 27

    Entity Framework 6 - foreign key issue

  28. 28

    Entity Framework 6 could not create a foreign key

  29. 29

    Entity Framework 6 Foreign Key Issue

HotTag

Archive