[英]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.我有一个 webform 来创建一个板,每当我尝试运行该站点时,我都会收到 NullReferenceException。
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:我正在尝试调用此方法将板添加到 User 类:
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.为了澄清起见,每个板都有一个“User_ID”,它应该是创建板的用户的 ID。
Edit 2:编辑2:
This is the layout of the table for Board这是 Board 的表格布局
It consists of an ID, a name, the date it was created and the ID of the user who created the board.它由 ID、名称、创建日期和创建板的用户 ID 组成。
That occurs because of you didn't initialized boardToAdd object, it's initial value is null.这是因为您没有初始化 boardToAdd 对象,它的初始值为 null。 Try creating instance of board.尝试创建板的实例。
static Board boardToAdd = new 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.编辑:我在您的代码中看不到 _context.SaveChanges()。 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.