2

How to create a foreign key into the below User class on the ID of the computer class?

User table:

[Table("User")]
class User{
 [Key]
 public int idUser;
 public int idComputer; //ForeignKey
}

Computer table:

[Table("Computer")]
class Computer{
 [Key]
 public int idComputer;
}

How to do a one-to-many relationship and one-to-one relationship between these classes, please?

1
  • This has nothing to do with MVC Commented Apr 14, 2014 at 16:01

2 Answers 2

4

To set it up so that a User has many Computers and a Computer has one User, you would do this:

class User {
    public int idUser {get;set;}

    public virtual ICollection<Computer> Computers {get;set;}
}

class Computer {
    public int idComputer {get;set;}

    public int UserID {get;set;} // automatically detected as a ForeignKey since the format it [classname]ID
    public virtual User User {get;set;}
}

To set it up so that a Computer has many Users and a User has one Computer, you would do:

class User {
    public int idUser {get;set;}

    public int ComputerID {get;set;}
    public virtual Computer Computer {get;set;}
}

class Computer {
    public int idComputer {get;set;}

    public virtual ICollection<User> Users {get;set;}
}

To set it up so that a User has one Computer:

class User {
    public int ID {get;set;}

    public virtual Computer Computer {get;set;}
}

class Computer {
    public int ID {get;set;}

    [Required]
    public int UserID {get;set;}
    public virtual User User {get;set;}
}

The [Required] tag should eliminate any errors about the one-to-one relationship

As a side note, the whole point of EF Code First is to make things super simple and quick and easy, but when you don't use some of the features it offers it diminishes it greatly and you have to do more manual work, which can lead to mistakes.

Here's a tutorial on how to do code-first. I recommend you read up on it.

Okay, I re-read your original question. You can't have both (one-to-one and one-to-many) between the same two classes, you can either have a one-to-one (i.e., a User has one computer and a computer has one user) or a one-to-many (i.e, a User has many computers and a computer has one user, or a computer has many users and a user has one computer.) It's a one or the other, not both, kind of thing. The fact that you think this is possible reinforces my statement that you really need to read that tutorial or read up on simple relational database structure

Sign up to request clarification or add additional context in comments.

16 Comments

I'm currently running EF 6.0.2 version.
Okay, then all of what I mentioned above will work. Back to EF4 it should work, I'm not familiar with most things before that. But I highly recommend into having it map some of the stuff automatically. EF Code First is designed to do that, so why not use it?
Using what? but what about the [ForeignKey("User")]?
Using the auto-detection features of EF. With the way I specified it in the code above there's no need to say [ForeignKey("User")], EF is smart enough to figure that out on its own.
Your solution isn't longer than EhsanSajjad's solution?
|
0

this is one to one relationship:

[Table("User")]
class User{
 [Key]
 public int idUser {get;set;}
 public virtual Computer Computer {get;set;}
}


[Table("Computer")]
class Computer{
 [Key]
 public int idComputer {get;set;}
 [ForeignKey("idUser")]
 public virtual User user {get;set;}
}

for one-to-many, here relationship is one user has multiple computers:

[Table("User")]
    class User{
     [Key]
     public int idUser {get;set;}
     public ICollection<Computer> Computers {get;set;}
    }

[Table("Computer")]
    class Computer{
     [Key]
     public int idComputer {get;set;}
     [ForeignKey("idUser")]
     public virtual User user {get;set;}
    }

Example:

using(var db = new yourDbContext())
{

User user = new User();

user.Name = "Name";

db.User.Add(user);

db.SaveChanges();

Computer computer = new Computer();

// set properties here if any

Computer.CopmuterName ="Dell";

Computer.user = user;

db.Computer.Add(Computer);

db.SaveChanges();
}

19 Comments

how to savechanges into the database? why adding the idUser and User instead of only idUser? what about idComputer and Computer instead of only idComputer ?
because with userid we also need row for user linked with it, EF automatically fetches user record as well related with the computer
but what about the [ForeignKey("User")]? Using your lines, will not more configurations?
now look at the edited version, it will clear you things
They need the { get; set; } modifiers
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.