Linq to SQL未将数据插入数据库 [英] Linq to SQL not inserting data onto the DB

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

问题描述

我这里的行为有些怪异,我正在互联网和SO上浏览,但没有找到回应.

I have a little / weird behaviour here and Im looking over internet and SO and I didn't find a response.

我必须承认这是我第一次使用数据库,我知道如何将它们与SQL一起使用,但从未真正使用过.

I have to admit that this is my first time using databases, I know how to use them with SQL but never used it actually.

无论如何,我的应用程序插入数据时遇到问题,我只是创建了一个非常简单的项目来进行测试,但还没有解决方案.

Anyway, I have a problem with my app inserting data, I just created a very simple project for testing that and no solution yet.

我有一个带有Sql Server的示例数据库

I have an example database with Sql Server

Id-int(身份主键) 名称-nchar(10)(不为null)

Id - int (identity primary key) Name - nchar(10) (not null)

该表称为人",简称为饼图.

The table is called "Person", simple as pie.

我有这个:

static void Main(string[] args)
{
    var db = new ExampleDBDataContext {Log = Console.Out};

    var jesus = new Person {Name = "Jesus"};

    db.Persons.InsertOnSubmit(jesus);
    db.SubmitChanges();

    var query = from person in db.Persons select person;

    foreach (var p in query)
    {
        Console.WriteLine(p.Name);
    }
}

如您所见,没有其他事物.

As you can see, nothing extrange.

它在控制台上显示了耶稣.但是,如果您看到表数据,则没有数据,只是空的.我评论了对象的创建和插入,并且foreach不会打印任何内容(正常情况下,数据库中没有数据)

It show Jesus in the console. But if you see the table data, there is no data, just empty. I comment the object creation and insertion and the foreach doesn't print a thing (normal, there is no data in the database)

奇怪的是,我在数据库中手动创建了一行,并且Id是2而不是1(Linq确实在玩数据库,但没有创建行吗?)

The weird thing is that I created a row in the database manually and the Id was 2 and no 1 (Was the linq really playing with the database but it didn't create the row?)

有日志:

插入[dbo].人

INSERT INTO [dbo].Person

VALUES(@ p0)

VALUES (@p0)

SELECT CONVERT(Int,SCOPE_IDENTITY())AS [value]

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]

-@ p0:输入NChar(大小= 10;精度= 0;比例= 0)[耶稣]

-- @p0: Input NChar (Size = 10; Prec = 0; Scale = 0) [Jesus]

-上下文:SqlProvider(Sql2005)模型:AttributedMetaModel构建:3.5.30729.4926

-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.4926

选择[t0].[Id],[t0].[名称] 来自[dbo].[人] AS [t0]

SELECT [t0].[Id], [t0].[Name] FROM [dbo].[Person] AS [t0]

-上下文:SqlProvider(Sql2005)模型:AttributedMetaModel构建:3.5.30729.4926

-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.4926

我真的很困惑,所有的博客/书籍都使用这种片段将元素插入数据库.

I am really confused, All the blogs / books use this kind of snippet to insert an element to a database.

感谢您的帮助.

推荐答案

首先;您是否有一个TransactionScope跨越了这个范围,您还没有提交?

First; do you have a TransactionScope that spans this, that you haven't committed.

但是,更客气的是:这是您要与系统(而不是单独的数据库服务器)一起部署的数据库文件吗?如果是这样,则每次您单击"build"等时,它将版本从解决方案文件夹复制到生成输出中(bin/debug或bin/release).

However, more liktely: is this a database file that you are deploying with the system (rather than a separate database server)? If so, every time you hit "build" etc, it copies the version from your solution folder into the build output (bin/debug or bin/release).

查看构建输出副本;查看那个是否已更新.

Look in the build output copy; see if that has been updated.

这篇关于Linq to SQL未将数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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