保存父项后尝试包含一对多关系项时发生NullReferenceException [英] NullReferenceException while trying to including a one-to-many relationship item after saving parent
问题描述
框架::我正在使用MVC 3 + EntityFramework 4.1代码优先.
Framework: I'm using using MVC 3 + EntityFramework 4.1 Code-First.
概念:一个立法实体有许多Provision实体.这个想法是,用户输入一个立法实体,将其保存,然后将其保存的函数将其传递给另一个函数,以查看该立法是否具有ShortTitle.如果是这样,则将其格式化为措词正确的字符串,并将其包括为立法的第一条规定,然后将更改保存到db.
Concept: One Legislation entity has many Provision entities. The idea is that the user enters a Legislation entity, that gets saved then the function that saves it passes it along to another function to see whether that Legislation has a ShortTitle. If it does, then it formats it into a properly worded string and includes it as the Legislation's first Provision, then saves the changes to db.
问题:问题是,我尝试用不同的方式对其进行编码,我不断收到NullReferenceException,并告诉我使用"new"关键字创建一个新的对象实例,并指向我到第二个功能中的savedLegislation.Provisions.Add(provision);
行.
Issue: The problem is, I've tried coding it in different ways, I keep getting a NullReferenceException, telling me to create a new object instance with the "new" keyword, and points me to the savedLegislation.Provisions.Add(provision);
line in my second function.
以下是有争议的两个功能,第一个功能保留了适当的立法:
Here are the two functions at issue, this first one saves the Legislation proper:
public Legislation Save(NewLegislationView legislation)
{
Legislation newLegislation = new Legislation();
// Simple transfers
newLegislation.ShortTile = legislation.ShortTile;
newLegislation.LongTitle = legislation.LongTitle;
newLegislation.BillType = legislation.BillType;
newLegislation.OriginatingChamber = legislation.OriginatingChamber;
newLegislation.Preamble = legislation.Preamble;
// More complicated properties
newLegislation.Stage = 1;
this.NumberBill(newLegislation); // Provides bill number
newLegislation.Parliament = db.LegislativeSessions.First(p => p.Ending >= DateTime.Today);
newLegislation.Sponsor = db.Members.Single(m => m.Username == HttpContext.Current.User.Identity.Name);
// And save
db.Legislations.Add(newLegislation);
db.SaveChanges();
// Check for Short titles
this.IncludeShortTitle(newLegislation);
// return the saved legislation
return newLegislation;
}
第一个调用的第二个函数负责检查ShortTitle是否不为空,并创建与该立法相关的Provision,然后保存更改.
And the second function which is invoked by the first one deals with checking whether ShortTitle is not empty and create a Provision that is related to that Legislation, then save changes.
public void IncludeShortTitle(Legislation legislation)
{
var savedLegislation = db.Legislations.Find(legislation.LegislationID);
if (savedLegislation.ShortTile.Any() && savedLegislation.ShortTile.ToString().Length >= 5)
{
string shortTitle = "This Act may be cited as the <i>" + savedLegislation.ShortTile.ToString() + "</i>.";
var provision = new Provision()
{
Article = Numbers.CountOrNull(savedLegislation.Provisions) + 1,
Proponent = savedLegislation.Sponsor,
Text = shortTitle
};
savedLegislation.Provisions.Add(provision);
db.SaveChanges();
}
}
我一直在研究SaveChanges()
的工作方式,以及它是否正确地返回更新后的实体,它确实可以(因为在第二个函数中查找它没有问题).如果它能正常工作,并且在第二个功能中找到了立法并重新创建了该规定,那么我看不出它不断散发出的空"引用是什么.
I've been researching how SaveChanges()
works and whether it is properly returning the updated entity, it does (since I get no issue looking it up in the second function). If it works properly, and the legislation is found and the provision is newly created in the second function, I don't see what is the "null" reference it keeps spitting out.
推荐答案
在这种情况下,空引用将为savedLegislation.Provisions
.当EF从db.Legislations.Find(...)
方法返回您的Legislation
实例时,Provisions
集合将不会初始化为新的List<Provision>
.
The null reference in this case would be savedLegislation.Provisions
. The Provisions
collection won't be initialized to a new List<Provision>
when EF returns your Legislation
instance from the db.Legislations.Find(...)
method.
我要尝试的第一件事是这样的:
The first thing I'd try is something like this:
var savedLegislation = db.Legislations
.Include("Provisions")
.First(l => l.LegislationID == legislation.LegislationID);
...但是我还考虑仅使用传递到方法中的legislation
实例,而不是再次从数据库中获取它.
... but I'd also consider just using the legislation
instance that was passed into the method rather than fetching it from the database again.
这篇关于保存父项后尝试包含一对多关系项时发生NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!