使用linq to SQL和EF6在数据库表中插入或添加字符串?

时间:2015-05-14 06:16:36

标签: entity-framework-6

我正在尝试学习LINQ to SQL和Entity Framework 6.只是尝试将字符串添加到数据库表中。

以下是我现在所得到的:

static void Main(string[] args)
{
        using (ResponsesEntities db = new ResponsesEntities())
        {
            try
            {
                Respons res = new Respons();
                res.ExtSerial = "ASDF1234";
                res.Date = "2015-05-01";
                res.FileName = @"C:\tae.XML";
                res.ResponseDescription = "OK";

                db.Responses.Add(res);

                Console.WriteLine("Inserted!");
                Console.ReadLine();
            }
            catch (Exception)
            {
                Console.WriteLine("TAE! Error");                    
            }
        }

它会运行但不会将字符串添加到表中。

1 个答案:

答案 0 :(得分:1)

您只是将对象添加到内存中商店 - 您需要坚持您的更改回数据库!

 db.Responses.Add(res);
 db.SaveChanges();   // this will write the changes to the database
相关问题