[英]WPF Entity Framework with multiple databases
I am developing a WPF application which uses different databases. 我正在开发使用不同数据库的WPF应用程序。 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. 我的问题是:数据库之一具有表
Projects
,其余数据库没有此表。 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: 我遇到的问题是,当启动应用程序并选择没有
Project
表的数据库,然后从组合框中选择具有project
表的数据库时,出现以下错误:
"System.NotSupportedException occurred HResult=0x80131515
“发生System.NotSupportedException HResult = 0x80131515
Message=The specified type member 'ProjectCode' is not supported in LINQ to Entities.Message = LINQ to Entities不支持指定的类型成员'ProjectCode'。 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";
}
}
}
I found a solution. 我找到了解决方案。 I have created separate DBContext for each database.
我为每个数据库创建了单独的DBContext。 As all databases are not quite identical this was a challenge.
由于所有数据库都不完全相同,因此这是一个挑战。 Context for each database works perfect
每个数据库的上下文都完美无缺
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.