I have a windows form app project in C#. I am using ORM tool that entity framework. I am beginner in EF. I have two entities ( Machine, User ) A user can have multiple machines, but a machine cannot have multiple users. I want to establish relationship like this. But right now my classes just have id and name property. The class structure I've created in its current state is not suitable for the task I mentioned because there is no relationship.
internal class Machine
{
[Key]
public int id { get; set; }
public string name { get; set; }
}
internal class User
{
[Key]
public int id { get; set; }
public string name { get; set; }
}
To establish the relationship I mentioned, what class structure do I need to create?
This is what I do
internal class Machine
{
[Key]
public int id { get; set; }
public string name { get => name; set => name = value; }
Public User User {get; set;}
}
internal class User
{
[Key]
public int id { get; set; }
public string name { get => name; set => name = value; }
Public List<Machine> Machines {get; set;}
}
And then Add-Migration [name your migration]
Like:Add-Migration one-to-many- relationship-user-machine And Update-Database