如果我这样做:
User userOOM = new User();
userOOM.Nickname = "markzzz";
UserFacebook userFacebook = new UserFacebook();
userFacebook.first_name = "marco";
db.User.InsertOnSubmit(userOOM);
userOOM.UserFacebook.Add(userFacebook);
db.SubmitChanges();
插入是完美的,它的工作原理。但是,如果我这样做:
User userOOM = new User();
userOOM.Nickname = "markzzz";
UserFacebookCatcher userCatched = new UserFacebookCatcher();
UserFacebook userFacebook = (UserFacebook)userCatched;
userFacebook.first_name = "marco";
db.User.InsertOnSubmit(userOOM);
userOOM.UserFacebook.Add(userFacebook);
db.SubmitChanges();
(所以,我投了userFacebook
类)我得到Object reference not set to an instance of an object.
例外。如果我删除它可以工作:
userOOM.UserFacebook.Add(userFacebook);
所以铸造就是问题所在。 UserFacebookCatcher
并不特别:
public class UserFacebookCatcher : UserFacebook
{
public UserFacebookCatcher()
{
this.first_name = "marco"
}
}
如果继承一个类,LINQ to SQL不起作用吗?