1

我正在尝试使用 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 |  |  |  |  | 

有任何想法吗?我什至不知道如何在没有线索的情况下开始

4

1 回答 1

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);
        }
    }
于 2013-08-01T15:20:24.953 回答