WPF Entity Framework with multiple databases

1

I am developing a WPF application which uses different databases. The database is selected from a combobox on every form and fills the data grid with the data.

The problem I have is this: one of the databases has a table Projects, the rest of the databases don't have this table. Depending on the database, there are two queries which fill the datagrid.

The problem I am having is when start the application and select the database which doesn't have Project table, then select the database which has the project table from the combobox, I am getting the following error:

"System.NotSupportedException occurred HResult=0x80131515
Message=The specified type member 'ProjectCode' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

Here is my code:

    private void GetTemplates()
    {
        List<string> result = new List<string>();
        if (CrsInterfaceDbDataContext.DbSelector == StringEnum.GetStringValue(DatabaseHelper.GiCrsUtms))
        {
            try
            {
                using (var db = new CrsInterfaceDbDataContext())
                {
                    string projectCode = CrsInterfaceDbDataContext.ProjectCode;

                    if (projectCode != null)
                    {
                        var templates = (from c in db.ClientProducts
                                         join ps in db.ClientProductStatuses on c.ClientProductStatusID equals ps
                                             .ClientProductStatusID
                                         join pr in db.Projects on c.ProjectCode equals pr.ProjectCode
                                         join s in db.ClientProductStatuses on c.ClientProductStatusID equals s
                                             .ClientProductStatusID
                                         join pc in db.ClientProductComponents on c.ClientProductID equals pc.ClientProductID
                                         join f in db.GiFilename on c.FileNameID equals f.FileNameID
                                         join fg in db.GiFileGroups on f.FileGroupID equals fg.FileGroupID
                                         join mp in db.GiMailParamses on fg.FileGroupID equals mp.FileGroupID
                                         join cco in db.ClientComponent on pc.ClientComponentID equals cco.ClientComponentID
                                         join ct in db.GiComponentType on cco.ClientComponentTypeID equals ct
                                             .ClientComponentTypeID
                                         where (c.ProjectCode == projectCode)

                                         select new
                                         {
                                             c.ClientProductID,
                                             c.ClientProductName,
                                             StatusDescription = ps.Description,
                                             c.MailingRegion,
                                             FileName = f.Filename,
                                             FileGroup = fg.Description,
                                             c.Description,
                                             c.Duplex,
                                             mp.Carrier,
                                             mp.ServiceLevel,
                                             pc.Qty,
                                             ClientComponetType = ct.Description,
                                             c.ProjectCode,
                                             pr.ProjectName
                                         }).ToList();

                        DgTemplates.ItemsSource = templates;
                        LblTotalRecords.Content = "Total records: " + templates.Count();

                    }


                }
            }
            catch (InvalidOperationException ex)
            {
                lblError.Content = "Database not selected";
            }
        }
        else
        {
            try
            {
                using (var db = new CrsInterfaceDbDataContext())
                {
                    var templates = (from c in db.ClientProducts
                                     join ps in db.ClientProductStatuses on c.ClientProductStatusID equals ps
                                         .ClientProductStatusID
                                     join s in db.ClientProductStatuses on c.ClientProductStatusID equals s.ClientProductStatusID
                                     join pc in db.ClientProductComponents on c.ClientProductID equals pc.ClientProductID
                                     join f in db.GiFilename on c.FileNameID equals f.FileNameID
                                     join fg in db.GiFileGroups on f.FileGroupID equals fg.FileGroupID
                                     join cco in db.ClientComponent on pc.ClientComponentID equals cco.ClientComponentID
                                     join ct in db.GiComponentType on cco.ClientComponentTypeID equals ct
                              .ClientComponentTypeID
                                     select new
                                     {
                                         c.ClientProductID,
                                         c.ClientProductName,
                                         StatusDescription = ps.Description,
                                         c.MailingRegion,
                                         FileName = f.Filename,
                                         FileGroup = fg.Description,
                                         c.Description,
                                         c.Duplex,
                                         ServiceLevel = c.Class,
                                         c.Carrier,
                                     }).ToList();

                    DgTemplates.ItemsSource = templates;
                    LblTotalRecords.Content = "Total records: " + templates.Count();



                }

            }
            catch (InvalidOperationException ex)
            {
                lblError.Content = "Database not selected";
            }
        }
    }
c#
wpf
entity-framework
linq
asked on Stack Overflow Oct 25, 2017 by user2693802 • edited Oct 25, 2017 by marc_s

1 Answer

0

I found a solution. I have created separate DBContext for each database. As all databases are not quite identical this was a challenge. Context for each database works perfect

answered on Stack Overflow Nov 13, 2017 by user2693802

User contributions licensed under CC BY-SA 3.0