问题描述
我已经使用LINQ To SQL已有一段时间了,而我通常在解决方案中的工作如下:
I've been working with LINQ To SQL for some time now, and what I usually do in a solution is the following:
- 在一个项目中,我创建一个dbml模式.
- 在另一个项目中,我创建一个简单的DataAccessLayer(DAL),它知道我的第一个项目,并实例化一个DataContext.
- 在第三个项目(业务逻辑)中,我实例化DAL.
这通常效果很好.但是,这一次,我不知道为什么,但是"It"不起作用. 它"是我正在更新数据库".我更改了代码以进行一些测试,但结果却是我不明白.
This usually works well. However, this time, I don't know why, but "It" doesn't work. "It" being "Me updating the database". I changed my code around to do some tests, and I get a result I don't understand.
MyDataContext dataContext = new MyDataContext(MyConnectionString);
DataBaseItem dbi = (from item in dataContext.DataBaseItems
where item.ID == 1
select item).First();
dbi.Name= "toto";
// dataContext.GetChangeSet() tells me nothing changed.
我更深入地研究了bdi.Name = "toto";
,并将其与工作所在的项目(都是设计人员生成的代码)中类似的值赋值进行了比较,发现缺少一些代码(我在此处写下了这些代码,但我评论了他们,以便您看到缺少的内容):
I dug deeper by breaking into the bdi.Name = "toto";
and compared it with a similar value assignment in a project where it works (both are designer generated code) and saw that some code was missing (I wrote them down there, but I commented them so you see what is missing) :
[Column(Storage="_Name", DbType="NVarChar(250)")]
public string Name
{
get
{
return this._Name;
}
set
{
if ((this._Name!= value))
{
//this.OnLayoutChanging(value);
//this.SendPropertyChanging();
this._Name= value;
//this.SendPropertyChanged("Name");
//this.OnLayoutChanged();
}
}
}
任何人都可以告诉我这些行为何丢失,我在哪里弄糟了?
Anyone can tell me how come these lines are missing, and where did I messed up?
当我执行dataContext.Refresh(RefreshMode.KeepChanges, dataContext.DataBaseItems);
时,出现错误:
When I do dataContext.Refresh(RefreshMode.KeepChanges, dataContext.DataBaseItems);
, I get an error:
推荐答案
该错误是在IDE(VS2008)中.IDE生成或重新生成的任何Linq-to-Sql对象都是错误的.要解决此问题,我必须:
The error was within the IDE (VS2008).The generation, or regeneration, of any Linq-to-Sql object by the IDE was faulty. To fix the problem, I had to :
- 删除错误的对象.
- 关闭Visual Studio.
- 重新打开Visual Studio.
- 重新创建对象.
不做第一站,而是通过更改参数来重新生成对象是可行的,但是由于我不知道如何重现该问题,所以我无法对其进行测试.
Not doing the first stop and instead have the object regenerated by changing a parameter could have worked, but I can't test it, since I don't know how to reproduce the problem.
这篇关于我怎么错了我的DataContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!