简体   繁体   中英

How to add a class to another class that has a one-to-many relationship

I have a system where users can log in and create boards. For this, there is the two separate classes 'User' and 'Board'.

public class User
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string UserType { get; set; }
    public DateTime LastLoginDate { get; set; }
    public virtual ICollection<Board> Boards { get; set; }
    public virtual ICollection<Post> Posts { get; set; }

public class Board
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime DateCreated { get; set; }
    public virtual ICollection<Post> Posts { get; set; }

I have a webform to create a board and I'm getting a NullReferenceException whenever I try to run the site.

public partial class AddBoard : System.Web.UI.Page
{
    Board board = new Board();
    User user = new User();
    Utility utility = new Utility();

    static User loggedInUser;
    static Board boardToAdd;

    protected void CreateButton_Click(object sender, EventArgs e)
    {
        string name = NameTextBox.Text;
        loggedInUser = (User)Session["loggedInUser"];

        string checkName = utility.CheckBoardName(name);
        if (checkName == "OK")
        {
            boardToAdd.Name = name;
            boardToAdd.DateCreated = DateTime.Now;
            user.AddBoard(boardToAdd, loggedInUser);


        }
        else
        {
            CreateLabel.Text = checkName;
        }
    }
}

The exception occurs on this line:

boardToAdd.Name = name;

I am trying to call this method to add the board to the User class:

public User AddBoard(Board board, User user)
    {
        BulletinContext _context = new BulletinContext();
        User _user = _context.Users.Find(user.ID);

        _context.Boards.Add(board);
        _context.Users.Add(user);
            return null;

    }

Edit: That error has been solved, but I'm still unable to create a board that is attached to the user. For clarification, each board has a 'User_ID' that should be the ID of the user who created the board.

Edit 2:

This is the layout of the table for Board

It consists of an ID, a name, the date it was created and the ID of the user who created the board.

That occurs because of you didn't initialized boardToAdd object, it's initial value is null. Try creating instance of board.

static Board boardToAdd = new Board();

It looks like you haven't defined foreign keys. So you'll need to do something like.

public User AddBoard(Board board, User user)
{
    BulletinContext _context = new BulletinContext();
    board.User_ID = user.ID;
    _context.Boards.Add(board);
        return null;

}

EDIT: I can't see a _context.SaveChanges() in your code. So you may need to add that if it isn't there.

If you have foreign keys defined:

Change the User Class to create a new HashSet of Boards something like:

public User() 
{
     Boards = new HashSet<Board>();
}
public User AddBoard(Board board, User user)
{
    BulletinContext _context = new BulletinContext();
    User _user = _context.Users.Find(user.ID);
    _user.Boards.Add(board);
    return null;

}

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