I'm starting with Entity Framework, and I'm wondering if I can access each of the tables from the main name = "DBEntities" approach?
This is my code, and it is not clear to me why I am not getting the data in table2 if I am already using a basic approach to the database.
App.config
<connectionStrings>
<add name="DBEntities"
connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-GN4506J;initial catalog=Test;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Form1.cs
public partial class FormMenu : Form
{
Table1 firstTable = new Table1 ();
Table2 secondTable = new Table2();
Table3 thirdTable = new Table3();
using (DBEntities db = new DBEntities())
{
try
{
firstTable = db.Table1.Where(x => x.BATCH_ID == cardUID).FirstOrDefault();
if (firstTable == null)
{
return;
}
else
{
thirdTable = db.Table3.Where(x => x.LPE_ID == firstTable.MA_ID).FirstOrDefault();
secondTable = db.Table2.Where(x => x.ZPZ_Datum.Value.Date == DateTime.Now.Date && x.ZPZ_LPE_ID == firstTable.MA_ID).LastOrDefault();
// here the application shoots me because in secondTable I don't get data based on db.Table2, where am I making a mistake?
if (thirdTable == null)
{
db.Entry(thirdTable ).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
}
}
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
};
}
error message :
'LINQ to Entities' does not recognize the 'ApplicationTest.Table2 LastOrDefault [Table2] (System.Linq.IQueryable`1 [ApplicationTest.Table2])' method, and this method cannot be translated into a memory expression.
Can I not access both tables this way? Why then do I get in the first db.Table1
data? Can anyone explain this to me and give me a solution.