[英]LINQ InsertOnSubmit: NullReferenceException
I have this code: 我有这个代码:
using DC = MV6DataContext;
using MV6; // Business Logic Layer
// ...
public DC.MV6DataContext dc = new DC.MV6DataContext(ConnectionString);
IP ip = new IP(Request.UserHostAddress);
dc.IPs.InsertOnSubmit(ip);
dc.SubmitChanges();
// in Business Logic layer:
public class IP : DC.IP {
public IP(string address) { ... }
}
Upon attempting to InsertOnSubmit(ip), I get a NullReferenceException (Object reference not set to an instance of an object). 在尝试InsertOnSubmit(ip)时,我得到一个NullReferenceException(对象引用未设置为对象的实例)。 dc is not null;
dc不为空; ip and all properties of ip are not null;
ip和ip的所有属性都不为null; though some are empty.
虽然有些是空的。
VS2008 won't let me step into InsertOnSubmit, so I have no way of knowing what specifically is null when being evaluated. VS2008不会让我进入InsertOnSubmit,因此在评估时我无法知道具体为null。 What gives?
是什么赋予了?
Note: I have checked, and all Linq.EntitySets created by FK relationships are present and non-null. 注意:我已经检查过,并且由FK关系创建的所有Linq.EntitySets都存在且非空。
Actually it's better to add a call to your constructor that also calls the generic constructor such as: 实际上最好添加对构造函数的调用,该构造函数也调用泛型构造函数,例如:
public IP(string address) : this() {
...
}
Got it. 得到它了。
Rather than creating a class that inherits from the DataContext's class, I extend the DC class itself with a partial class in the Business Logic layer. 我没有创建一个继承自DataContext类的类,而是使用业务逻辑层中的部分类扩展DC类本身。 From there I can add whatever constructors and methods I wish.
从那里我可以添加我想要的任何构造函数和方法。
In this case, it is neccessary to copy the code from the existing (auto-generated) constructor: 在这种情况下,有必要从现有(自动生成)构造函数中复制代码:
public IP(string address) {
Address = address;
Domain = "";
Notes = "";
FirstAccess = DateTime.Now;
LastAccess = DateTime.Now;
this._Sessions = new EntitySet<Session>(new Action<Session>(this.attach_Sessions), new Action<Session>(this.detach_Sessions));
OnCreated(); }
Not sure what's in that OnCreated handler, but it seems to be doing the work that boned me earlier. 不确定那个OnCreated处理程序中有什么,但它似乎正在做我之前的工作。 Works fine now :)
工作正常:)
Since the default constructor already initializes base(), this._Sessions and runs the OnCreated method, all you need to do in your extended constructor is this: 由于默认构造函数已初始化base(),this._Sessions并运行OnCreated方法,因此您需要在扩展构造函数中执行以下操作:
public IP(string address) : this()
{
Address = address;
Domain = "";
Notes = "";
FirstAccess = DateTime.Now;
LastAccess = DateTime.Now;
}
Is this a designer generated DataContext or your own hand-built one. 这是设计器生成的DataContext还是您自己手工构建的DataContext。 I suspicious that the IPs table may not be instantiated at the time you try your
InsertOnSubmit()
. 我怀疑在您尝试
InsertOnSubmit()
时可能无法实例化IP表。 I can't see how this would happen with a designer-generated DataContext, but I've been known to forget to initialize my collections from time to time in my own code. 我无法看到设计器生成的DataContext会如何发生这种情况,但我已经知道忘记在我自己的代码中不时初始化我的集合。
I had a slightly different situation than the asker, but got the same error for the same reasons. 我的情况与提问者略有不同,但出于同样的原因也出现了同样的错误。 I wrote new constructors in a partial class for my database entities, then tried to use the resulting objects in
InsertOnSubmit
calls. 我在我的数据库实体的分部类中编写了新的构造函数,然后尝试在
InsertOnSubmit
调用中使用结果对象。
None of these answers helped me directly, but I was able to figure out what they were getting at after reading all of them. 这些答案都没有直接帮助我,但我能够在阅读完所有内容后弄清楚他们得到了什么。
The auto-generated parameterless constructor for the entity does things that need to happen for InsertOnSubmit
to work, so if you overload the constructor -- like me -- or inherit from the class -- like the asker -- you need to call the base constructor from your new constructor, like so: 实体的自动生成的无参数构造函数执行
InsertOnSubmit
需要发生的事情,所以如果你重载构造函数 - 比如我 - 或者从类继承 - 比如提问者 - 你需要调用base来自新构造函数的构造函数,如下所示:
public partial class Entity {
public Entity( Type parameter ) : this() {
// do things with the parameter
}
}
or 要么
public class SubEntity: Entity {
public SubEntity( Type parameter ) : base() {
// do things with the parameter
}
}
您可以尝试查看正在发生的事情,将要做什么更改,如果您在SubmitChanges之前放置一个断点,并快速观察dc.GetChangeSet() 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.