Tak jsem zase neuspěl. Dotaz jak si představuju, že měl fungovat:
var doc = (from d in dc.Documents
where d.Name == "TestDocument"
select d).Single();
var row = (from r in dc.Rows
select r).First();
//row se ale nevykoná, protože v dokumentu kolekce Rows je prázdná.
return row.GCodes.ToList();
Přikládám celý testovací projekt. Tabulky GCodes a MCodes, jsou plněny SQL dávkou mimo aplikaci. Modelové třídy:
class Document
{
[Key]
public int DocumentId { get; set; }
[Required, StringLength(50)]
public string Name { get; set; }
[StringLength(200)]
public string Description { get; set; }
[Required]
public DateTime Created { get; set; }
[Required]
public DateTime Saved { get; set; }
public virtual ICollection<Row> Rows { get; set; }
public Document()
{
Rows = new HashSet<Row>();
}
}
class Row
{
[Key]
public int RowId { get; set; }
public Single? X { get; set; }
public Single? Y { get; set; }
public Single? Z { get; set; }
public Single? A { get; set; }
public Single? B { get; set; }
public Single? C { get; set; }
public Single? I { get; set; }
public Single? J { get; set; }
public int? T { get; set; }
public int? F { get; set; }
public int? S { get; set; }
[StringLength(50)]
public string Comment { get; set; }
[Required]
public int RowNumber { get; set; }
public int DocumentId { get; set; }
public virtual Document Document { get; set; }
public virtual ICollection<MCode> MCodes { get; set; }
public virtual ICollection<GCode> GCodes { get; set; }
public Row()
{
MCodes = new HashSet<MCode>();
GCodes = new HashSet<GCode>();
}
}
class GCode
{
[Key]
public int GCodeId { get; set; }
[Required]
public int Number { get; set;}
[StringLength(50)]
public string Comment { get; set; }
[StringLength(200)]
public string Description { get; set; }
[Required]
public bool Used { get; set; }
public virtual ICollection<Row> Rows { get; set; }
public GCode()
{
Rows = new HashSet<Row>();
}
}
class MCode
{
[Key]
public int MCodeId { get; set; }
[Required]
public int Number { get; set;}
[StringLength(50)]
public string Comment { get; set; }
[StringLength(200)]
public string Description { get; set; }
[Required]
public bool Used { get; set; }
public virtual ICollection<Row> Rows { get; set; }
public MCode()
{
Rows = new HashSet<Row>();
}
}
class CmsContext: DbContext
{
public CmsContext()
: base(nameOrConnectionString: "ProgramDatabaseEntities")
{}
public DbSet<Document> Documents { get; set; }
public DbSet<Row> Rows { get; set; }
public DbSet<GCode> GCodes { get; set; }
public DbSet<MCode> MCodes { get; set; }
}
Přidání testovacího dokumentu:
private static void AddTestDocument(Data.CmsContext dc)
{
var doc = new Data.Document
{
Name = "TestDocument",
Created = DateTime.Now,
Saved = DateTime.Now,
Description = "popis",
};
dc.Documents.Add(doc);
dc.SaveChanges();
}
Přidání testovacích řádků:
private static void AddTestRow(Data.CmsContext dc)
{
var doc = (from d in dc.Documents
where d.Name == "TestDocument"
select d).Single();
var g00 = (from g in dc.GCodes
where g.Number == 0
select g).Single();
var g01 = (from g in dc.GCodes
where g.Number == 1
select g).Single();
var g90 = (from g in dc.GCodes
where g.Number == 90
select g).Single();
var g91 = (from g in dc.GCodes
where g.Number == 91
select g).Single();
var m3 = (from m in dc.MCodes
where m.Number == 3
select m).Single();
var m8 = (from m in dc.MCodes
where m.Number == 8
select m).Single();
var r1 = new Row
{
X = 1000,
Y = 500,
Z = 600,
Document = doc,
RowNumber = 10,
Comment = "Komentář",
MCodes = new HashSet<MCode>(),
GCodes = new HashSet<GCode>(),
T = 5,
};
r1.MCodes.Add(m3);
r1.MCodes.Add(m8);
r1.GCodes.Add(g00);
r1.GCodes.Add(g90);
var r2 = new Row
{
A = 600,
B = 350,
Document = doc,
C = 658.98F,
RowNumber = 20,
Comment = "Komentář",
T = 5,
MCodes = new HashSet<MCode>(),
GCodes = new HashSet<GCode>()
};
r2.MCodes.Add(m3);
r2.MCodes.Add(m8);
r2.GCodes.Add(g01);
r2.GCodes.Add(g91);
dc.Rows.Add(r1);
dc.Rows.Add(r2);
}
|