I am using MVC C# I have a model:
public class AccountModel
{
[Key]
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public List<GameModel> Apps { get; set; }
public bool IsUseless { get; set; }
}
and a GameModel:
public class GameModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public bool VacBanned { get; set; }
}
And i am trying to store in Account database, using Entity an Account that has N games. But the Entity Framework doesn't even create a Column named Apps and i have no idea how to fix that.
CodePudding user response:
I'd add some navigation properties to those models.
public class AccountModel
{
[Key]
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public ICollection<GameModel> Apps { get; set; }
public bool IsUseless { get; set; }
}
public class GameModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public bool VacBanned { get; set; }
[ForeignKey(nameof(Account))]
public int AccountId { get; set; }
public AccountModel Account { get; set; }
}
Provided you have your DBContext set up correctly (which you haven't provided here) the above should automatically map the one to many relationship between AccountModel and GameModel. Don't forget if this is the first time you are using the model and if you are using code first rather than mapping to an existing database you will need to add and apply migrations to create the database.
See https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=dotnet-core-cli for details.