我试图使用Entity Framework从数据库中读取记录,我之前没有使用过。
我试图运行加载规则方法:
class Rule
{
private STRATCODE stratCode;
private STRATRULEDEF stratRuleDef;
AREEntities AREDb = new AREEntities();
public Rule(STRATRULEDEF StratRuleDef, STRATCODE StratCode)
{
this.stratRuleDef = StratRuleDef;
this.stratCode = StratCode;
}
public void LoadRule()
{
var query = from stratCode in AREDb.STRATCODES
where stratCode.CODEVALUE == "49300"
select stratCode;
//StratCodeRepository StratCodeRepository = new StratCodeRepository(new AREEntities());
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\user\Desktop\NewText.txt",true))
{
foreach (STRATCODE row in query)
{
file.WriteLine("{0} | {1} | {2} | {3} | {4} | {5}", stratCode.STRATRULEKEY, stratCode.CODETYPE, stratCode.CODEVALUE, stratCode.SYSTEMID, stratCode.CODESETKEY, stratCode.PAYMENTYEAR);
}
}
}
我的问题是,每次运行它时,它都返回正确数量的记录,但文本文件中的所有数据都是空的,我在输出日志中找不到任何线索。
0 | | | | |
0 | | | | |
0 | | | | |
0 | | | | |
0 | | | | |
有什么想法吗?我不知道如何开始没有线索
答案 0 :(得分:4)
您的写入不使用foreach变量:
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\stephen.carmody\Desktop\NewText.txt", true))
{
foreach (var row in query)
{
file.WriteLine("{0} | {1} | {2} | {3} | {4} | {5}",
row.STRATRULEKEY,
row.CODETYPE,
row.CODEVALUE,
row.SYSTEMID,
row.CODESETKEY,
row.PAYMENTYEAR);
}
}