I am working with .NET Core 2 MVC and I have the following models:
BooksCategories Model:
public class BooksCategories
{
[Key]
public int BookCategoryId { get; set; }
[Required]
public string Name { get; set; }
public bool isSelected { get; set; }
}
FirstInput Model:
public class FirstInput
{
//[Key]
public int FirstInputId { get; set; }
public string UserId { get; set; }
[ForeignKey("UserId")]
public virtual ApplicationUser User { get; set; }
public bool Books { get; set; }
public string SelectedBookCategories { get; set; }
public List<BooksCategories> BookCategoriesObj { get; set; }
}
My idea is when the user first registers into an an application, he/she is redirected to a form-page to complete his profile by ticking his favorite categories. The categories inside the View are populated from the database.
However, when I did the migration, I got the following result:
migrationBuilder.CreateTable(
name: "BooksCategories",
columns: table => new
{
BookCategoryId = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
FirstInputId = table.Column<int>(nullable: true),
Name = table.Column<string>(nullable: false),
isSelected = table.Column<bool>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_BooksCategories", x => x.BookCategoryId);
table.ForeignKey(
name: "FK_BooksCategories_UserFirstInput_FirstInputId",
column: x => x.FirstInputId,
principalTable: "UserFirstInput",
principalColumn: "FirstInputId",
onDelete: ReferentialAction.Restrict);
});
The framework has added another column in the BookCategories table called FirstInputId that is dependent with an indexer from the FirstInput model. This has seriously messed up my user generated entries. I don't want user data to be stored inside the BooksCategories table, I only want to fetch it from the db. Then they will be saved as multiple answers in a string in the FirstInput table.
What have I done wrong? P.S. I tried to find something similar but nothing was close to the problem I am having.