problem wth migration blazor wasm
sblb
1,171
Reputation points
Hi, I have two class that created the Foregin Key.
One think I don't understand when i've make the migration a new column is created ProjetModelIdProjet then I didn't define this column.
When I invoke put method eg the column ProjetModelIdProjet
is filled and the IdProjet = 0
If the IdProjet
is not filled there will be problems with different methods.
How I can fix this problem?
I tried to delete the [key] attribute but the migrations doesn't work in this case.
thanks in advance to your help!
public class ProjetModel
{
[Key]
public int IdProjet { get; set; }
[Required]
public DateTime DateProjet { get; set; }
[Required]
public string? Title { get; set; }
public string? Type { get; set; }
public string? Level { get; set; }
public bool? IsCompleted { get; set; }
public string? Statut { get; set; }
public int ProjetCount { get; set; }
public string? ProjetCountId {
get
{
return $"{ProjetCount.ToString().PadLeft(3, '0')}";
}
set { }
}
[Timestamp]
public byte[]? ConcurrencyToken { get; set; }
public List<ActionItemProjet>? ActionItemProjets { get; set; }
}
public class ActionItemProjet
{
[Key]
public int ActionId { get; set; }
public string? Tilte { get; set; }
public string? DescriptionA { get; set; }
public string? State { get; set; }
public DateTime? OpenDate { get; set; }
public DateTime? PlanDate { get; set; }
public DateTime? CloseDate { get; set; }
public int IdProjet { get; set; }
[JsonIgnore]
public ProjetModel? ProjetModel { get; set; }
}
In applicationDbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ActionItemProjet>()
.HasOne(p => p.ProjetModel)
.WithMany(b => b.ActionItemProjets);
}
below the results of the migrations
migrationBuilder.CreateTable(
name: "ActionItemProjets",
columns: table => new
{
ActionId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Tilte = table.Column<string>(type: "nvarchar(max)", nullable: true),
DescriptionA = table.Column<string>(type: "nvarchar(max)", nullable: true),
State = table.Column<string>(type: "nvarchar(max)", nullable: true),
OpenDate = table.Column<DateTime>(type: "datetime2", nullable: true),
PlanDate = table.Column<DateTime>(type: "datetime2", nullable: true),
CloseDate = table.Column<DateTime>(type: "datetime2", nullable: true),
IdProjet = table.Column<int>(type: "int", nullable: false),
ProjetModelIdProjet = table.Column<int>(type: "int", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_ActionItemProjets", x => x.ActionId);
table.ForeignKey(
name: "FK_ActionItemProjets_ProjetModels_ProjetModelIdProjet",
column: x => x.ProjetModelIdProjet,
principalTable: "ProjetModels",
principalColumn: "IdProjet");
});
Sign in to answer