使用上下文插入数据库不起作用 [英] insert into database using context doesn't work

查看:119
本文介绍了使用上下文插入数据库不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在表格中插入值。
我这样做如下:

  JobTable jobTable = new JobTable(); 
int结果= -1;

JobEntities上下文= new JobEntities();
试试
{
jobTable.JobDate = DateTime.Now;
jobTable.JobProcess = processName;

context.JobTable.Attach(jobTable);
context.JobTable.Add(jobTable);

结果= context.SaveChanges();
Console.WriteLine( Result => + result.ToString());
}
获胜(例外)
{
抛出;
}

return jobTable.JobId;

我每次都获得一个新的JobId,但是当我检查数据库时,该表保持为空。
我缺少了什么?

解决方案

几件事,



首先附加通常用于告诉EF将数据库中已有的实体连接到上下文。您可以使用它进行添加,但是您需要手动告诉EF使用状态管理器添加了该实体。这不是您要在此处执行的操作。



添加是在数据库中插入行的常规方法,从本质上讲,这也将实体添加到EFs上下文中。 / p>

您应该在上下文周围使用using块,以确保其正确处理。这一点尤其重要,因为EF会保留所有实体的副本以跟踪发生了什么变化,因此不处理上下文会导致严重的内存泄漏。



以下是我的想法您的代码应如下所示:

  JobTable jobTable = new JobTable(); 

using(var context = new JobEntities())
{
jobTable.JobDate = DateTime.Now;
jobTable.JobProcess = processName;

context.JobTable.Add(jobTable);

var结果= context.SaveChanges();
Console.WriteLine( Result => + result.ToString());

return jobTable.JobId;
}

如果仍然无法执行,建议您查看连接字符串并确认它实际上已进入您期望的数据库,也许是在其他地方创建了自己的数据库



使用该应用程序查询表并查看您的数据库是否值得行,如果它设置的ID表示它实际上已进入SQL,因为ID通常是由数据库生成的


I'm trying to insert values in a table. I'm doing it as follows :

JobTable jobTable = new JobTable();
        int result = -1;

        JobEntities context = new JobEntities();
        try
        {
            jobTable.JobDate = DateTime.Now;
            jobTable.JobProcess = processName;

            context.JobTable.Attach(jobTable);
            context.JobTable.Add(jobTable);

            result = context.SaveChanges();
            Console.WriteLine("Result => " + result.ToString());
        }
        catch (Exception)
        {
            throw;
        }

        return jobTable.JobId;

I'm getting a new JobId each time, but when I check my database, the table stays empty. What am I missing ?

解决方案

A couple of things,

Firstly attach is normally used to tell EF to connect an entity which is already in the database to the context. You can use this for an add but you would need to manually tell EF that this entity is added using the state manager. This is not what you want to do here.

Add is the normal way to insert a row into the database, this also in essence adds that entity to EFs context.

You should use a using block around your context to ensure its correctly disposed. This is especially important as EF keeps a copy of all of your entities to track what has changed so not disposing contexts can lead to serious memory leaks.

Below is what i think your code should look like:

JobTable jobTable = new JobTable();

using(var context = new JobEntities())
{
    jobTable.JobDate = DateTime.Now;
    jobTable.JobProcess = processName;

    context.JobTable.Add(jobTable);

    var result = context.SaveChanges();
    Console.WriteLine("Result => " + result.ToString());

    return jobTable.JobId;
}

If this is still not working i would suggest you look at your connection strings and confirm its actually going to the DB you are expecting, maybe its made its own DB somewhere else

It may also be worthwhile using the app to query the table and see if your rows are there, if its setting an ID that means it has actually gotten to SQL as IDs are normally generated by the database

这篇关于使用上下文插入数据库不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆