Entity Framework Code First initializing foreign keys

Amai

This is how the code looks like:

public class Family
{
    public int FamilyID { get; set; }
    public virtual ICollection<Member> FamilyMembers { get; set; }
}
public class Member
{
    public int MemberID { get; set; }
    public virtual Family Family { get; set; }
}
public class Bicycle
{
    public int BicycleID { get; set; }
    public virtual Member Owner { get; set; }
}
public class MyContext : DbContext
{
    public MyContext : base () { }
}

So, there is a one-to-one relantionship between Member and Bicycle, and there is one-to-many relationship between Family and Member. My question is: how do I initialize these foreign keys using my context? All the tutorials on the internet say how to define, but now how to initialize them. Do I simply assign them a correct int value or do I assign them a whole object, and if so, how do I do that? Using LINQ?

@Edit: Assume there are 2 families: Smith and Johnson. Smith has 3 members: Ben, Amy, Brian. Johnson has 2 members: John and Natalie. How do I initialize all that in my code?

Steve Greene

First, fix this:

public class Member
{
    public int MemberID { get; set; }
    public virtual Family Family { get; set; }
}

That should be enough to configure the relationships. EF will make the keys identity by convention.

You can assign the relationships multiple ways. You can explicitly set FK Ids if you add them to your models, or you can use the navigation properties like this:

var family = new Family { LastName = "Smith", ...};
List<Member> members = new List<Member>
{
    new Member(){ Name = "Ben", ... },
    new Member(){ Name = "Amy", ... },
    new Member(){ Name = "Brian", ... }
};
family.FamilyMembers = members;
context.Family.Add(family);
context.SaveChanges();

To assign a bike to an owner:

var owner = context.Members.Find(ownerId);
var bike = new Bicycle
{
    Make = "ACME",
    Model = "XXXX",
    Owner = owner
};
context.Bicycles.Add(bike);
context.SaveChanges();

EDIT: Yes, it is certainly permissible to add FK to your model. That's the method I use. Like this code:

public class Member
{
    public int MemberID { get; set; }
    public int FamilyID { get; set; }  // EF will automatically make this a FK by convention
    public virtual Family Family { get; set; }
}

You need to add annotations if you don't adhere to convention or if you want them for documentation sake:

public class Member
{
    public int MemberID { get; set; }
    public int FamilyID { get; set; }
    [ForeignKey("FamilyID")]
    public virtual Family Family { get; set; }
}

See http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx

I prefer the fluent api. It keeps my models cleaner and separates concerns. For simple projects you can add all the fluent code in the OnModelCreating() override in your context, but I prefer to store my entity configurations in a folder under my context (one file per entity) as described here: http://odetocode.com/blogs/scott/archive/2011/11/28/composing-entity-framework-fluent-configurations.aspx

You can actually have EF automatically find your fluent code using the technique described here: https://msdn.microsoft.com/en-us/magazine/dn519921.aspx

Regarding the child collections, yes, you can new it up in the constructor:

public class Family
{
    public Family()
    {
        FamilyMembers = new HashSet<Member>();
    }

    public int FamilyID { get; set; }
    public virtual ICollection<Member> FamilyMembers { get; set; }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

(Code First) Entity Framework foreign keys using non standard naming convention

From Dev

Code-first Entity Framework - Multiple Foreign Keys For the Same Table

From Dev

Code First entity framework and foreign keys

From Dev

Entity Framework loading foreign keys with HasOptional

From Dev

Entity Framework Code First Foreign Key adding Index as well

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Entity Framework multiple foreign keys to the same table

From Dev

Foreign Keys in Entity Framework 6.1.2

From Dev

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

From Dev

Entity Framework Code first adds unwanted foreign key column

From Dev

Entity Framework 6 Code First Foreign Key Without Corresponding Properties

From Dev

Multiple Foreign Key for Same table in Entity Framework Code First

From Dev

Entity Framework Mapping. Multiple Foreign keys

From Dev

Entity Framework Code First and Firebird - Foreign Key name issue

From Dev

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

From Dev

Entity Framework Code First, Clustered Indexes & Composite Keys

From Dev

Get foreign key value using Entity Framework code first

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Entity Framework one to optional foreign key code first fluent mapping

From Dev

Foreign Key in Code First Entity Framework

From Dev

Entity Framework multiple foreign keys to the same table

From Dev

Entity framework code first cant create primary and foreign key relationship

From Dev

Entity Framework Code First, Clustered Indexes & Composite Keys

From Dev

Entity framework code first, get access to foreign key value

From Dev

Entity Framework: Foreign Key in code first

From Dev

Entity framework code first, custom foreign key name for inheritance

From Dev

Using Entity Framework Code First to have two Foreign Keys from same parent table without having to specify the collections on the parent entity

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

Related Related

  1. 1

    (Code First) Entity Framework foreign keys using non standard naming convention

  2. 2

    Code-first Entity Framework - Multiple Foreign Keys For the Same Table

  3. 3

    Code First entity framework and foreign keys

  4. 4

    Entity Framework loading foreign keys with HasOptional

  5. 5

    Entity Framework Code First Foreign Key adding Index as well

  6. 6

    Entity Framework Code First Foreign Key issue

  7. 7

    Entity Framework multiple foreign keys to the same table

  8. 8

    Foreign Keys in Entity Framework 6.1.2

  9. 9

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

  10. 10

    Entity Framework Code first adds unwanted foreign key column

  11. 11

    Entity Framework 6 Code First Foreign Key Without Corresponding Properties

  12. 12

    Multiple Foreign Key for Same table in Entity Framework Code First

  13. 13

    Entity Framework Mapping. Multiple Foreign keys

  14. 14

    Entity Framework Code First and Firebird - Foreign Key name issue

  15. 15

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

  16. 16

    Entity Framework Code First, Clustered Indexes & Composite Keys

  17. 17

    Get foreign key value using Entity Framework code first

  18. 18

    Entity Framework Code First Foreign Key issue

  19. 19

    Entity Framework one to optional foreign key code first fluent mapping

  20. 20

    Foreign Key in Code First Entity Framework

  21. 21

    Entity Framework multiple foreign keys to the same table

  22. 22

    Entity framework code first cant create primary and foreign key relationship

  23. 23

    Entity Framework Code First, Clustered Indexes & Composite Keys

  24. 24

    Entity framework code first, get access to foreign key value

  25. 25

    Entity Framework: Foreign Key in code first

  26. 26

    Entity framework code first, custom foreign key name for inheritance

  27. 27

    Using Entity Framework Code First to have two Foreign Keys from same parent table without having to specify the collections on the parent entity

  28. 28

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

  29. 29

    Exchanged Foreign Key on Entity Framework Code First From data base

HotTag

Archive