LINQ to SQL不支持强制转换?

时间:2013-09-12 14:35:02

标签: c# .net inheritance linq-to-sql casting

如果我这样做:

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

0 个答案:

没有答案