Hallo,
Ich habe in meiner wpf-Anwendung folgende Models:
public class SchoolClass : ISchoolClassData
{
[Required]
public int SchoolClassId { get; set; }
[Required]
public SchoolClassInternal SchoolClassInternals { get; set; } = new SchoolClassInternal();
}
public class SchoolClassInternal : ISchoolClassInternal
{
[Required]
public int SchoolClassInternalId { get; set;}
[Required]
[MaxLength(64)]
public string SchoolClassLabel { get; set; }
[MaxLength(64)]
public string SchoolClassLevel { get; set; }
[MaxLength(64)]
public string SchoolClassHead { get; set; }
public ObservableCollection<Student> ClassStudents { get; set; } = new ObservableCollection<Student>();
}
Wenn ich ein SchoolClass-Objekt erstelle und es an die SQLite-Datenbank übergebe, dann finde ich die übergebenen Daten dort wieder. Beim Lesen der Daten jedoch sind die Properties von SchoolClassInternals leer. Meine Methode:
public static List<SchoolClass> GetAllClasses()
{
List<SchoolClass> _entity = new List<SchoolClass>();
using (var context = new DataBaseContext())
{
if (context.SchoolClasses.Any())
{
_entity = context.SchoolClasses
.Include(c => SchoolClassInternals)
.ToList();
}
return _entity;
}
Es werden zwar die richtigen SchoolClass-Objekte zurückgegeben, deren SchoolClassInternals-Properties sind jedoch leer (SchoolClassInternalId, SchoolClassInternalLabel, SchoolClassInternalLevel, SchoolClassInternalHead).
Ich habe mal den UP-Part der Migration kopiert - soweit ich das interpretieren kann, wurden alle Indizes richtig zugeordnet
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Adresses",
columns: table => new
{
AdressId = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Street = table.Column<string>(type: "TEXT", maxLength: 128, nullable: true),
PostalCode = table.Column<string>(type: "varchar(12)", maxLength: 10, nullable: true),
City = table.Column<string>(type: "TEXT", maxLength: 128, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Adresses", x => x.AdressId);
});
migrationBuilder.CreateTable(
name: "Educationals",
columns: table => new
{
EducationalId = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
ClassLabel = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
ClassLevel = table.Column<string>(type: "TEXT", maxLength: 64, nullable: true),
EducationalProgram = table.Column<string>(type: "TEXT", maxLength: 64, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Educationals", x => x.EducationalId);
});
migrationBuilder.CreateTable(
name: "Personals",
columns: table => new
{
PersonalId = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
FirstName = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
LastName = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
FullName = table.Column<string>(type: "TEXT", maxLength: 128, nullable: true),
DateOfBirth = table.Column<string>(type: "varchar(15)", maxLength: 15, nullable: true),
Gender = table.Column<string>(type: "TEXT", maxLength: 20, nullable: true),
PhoneNumber = table.Column<string>(type: "varchar(128)", maxLength: 128, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Personals", x => x.PersonalId);
});
migrationBuilder.CreateTable(
name: "SchoolClassInternals",
columns: table => new
{
SchoolClassInternalId = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
SchoolClassLabel = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
SchoolClassLevel = table.Column<string>(type: "TEXT", maxLength: 64, nullable: true),
SchoolClassHead = table.Column<string>(type: "TEXT", maxLength: 64, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_SchoolClassInternals", x => x.SchoolClassInternalId);
});
migrationBuilder.CreateTable(
name: "SchoolClasses",
columns: table => new
{
SchoolClassId = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
SchoolClassInternalsSchoolClassInternalId = table.Column<int>(type: "INTEGER", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_SchoolClasses", x => x.SchoolClassId);
table.ForeignKey(
name: "FK_SchoolClasses_SchoolClassInternals_SchoolClassInternalsSchoolClassInternalId",
column: x => x.SchoolClassInternalsSchoolClassInternalId,
principalTable: "SchoolClassInternals",
principalColumn: "SchoolClassInternalId",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable(
name: "Students",
columns: table => new
{
StudentId = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
StudentPersonalPersonalId = table.Column<int>(type: "INTEGER", nullable: true),
StudentAdressAdressId = table.Column<int>(type: "INTEGER", nullable: true),
StudentEducationalEducationalId = table.Column<int>(type: "INTEGER", nullable: true),
SchoolClassInternalId = table.Column<int>(type: "INTEGER", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Students", x => x.StudentId);
table.ForeignKey(
name: "FK_Students_Adresses_StudentAdressAdressId",
column: x => x.StudentAdressAdressId,
principalTable: "Adresses",
principalColumn: "AdressId",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Students_Educationals_StudentEducationalEducationalId",
column: x => x.StudentEducationalEducationalId,
principalTable: "Educationals",
principalColumn: "EducationalId",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Students_Personals_StudentPersonalPersonalId",
column: x => x.StudentPersonalPersonalId,
principalTable: "Personals",
principalColumn: "PersonalId",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Students_SchoolClassInternals_SchoolClassInternalId",
column: x => x.SchoolClassInternalId,
principalTable: "SchoolClassInternals",
principalColumn: "SchoolClassInternalId",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_SchoolClasses_SchoolClassInternalsSchoolClassInternalId",
table: "SchoolClasses",
column: "SchoolClassInternalsSchoolClassInternalId");
migrationBuilder.CreateIndex(
name: "IX_Students_SchoolClassInternalId",
table: "Students",
column: "SchoolClassInternalId");
migrationBuilder.CreateIndex(
name: "IX_Students_StudentAdressAdressId",
table: "Students",
column: "StudentAdressAdressId");
migrationBuilder.CreateIndex(
name: "IX_Students_StudentEducationalEducationalId",
table: "Students",
column: "StudentEducationalEducationalId");
migrationBuilder.CreateIndex(
name: "IX_Students_StudentPersonalPersonalId",
table: "Students",
column: "StudentPersonalPersonalId");
}
Habe ich etwas offensichtliches übersehen?
Gruß
Vorph