2

如果我这样做:

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 不起作用?

4

0 回答 0