简体   繁体   中英

asp.net How to insert record into database

I am having difficulties on getting my insert method to work correctly. I am new to back end development so any suggestions or comments will be helpful.

public Boolean insertDefaultUser()
    {
        Boolean flag = true;
        Users newUser = new Users();
        newUser.alias = "bulby";
        newUser.password = "chicken";
        newUser.email = "r@hot.com";

        dbc.Users.AddObject(newUser);   // ERROR !
        dbc.SaveChanges();
        return flag;

    }

However, on the "add object line" it gives me the follow error --->

Error "The best overloaded method match for System.Data.Objects.ObjectSet<Guild_Chat.User>.AddObject(Guild_Chat.User)' has some invalid arguments ".

Try adding the actual entity type Guild_Chat.User , for example:

public bool InsertDefaultUser()
{
    try
    {
       Guild_Chat.User newUser = new Guild_Chat.User
                              {
                                 alias = "bulby",
                                 password = "chicken",
                                 email = "r@hot.com"
                              };
       dbc.Users.AddObject(newUser);
       dbc.SaveChanges();
       return true;
    }
    catch(Exception e)
    {
       return false;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM