简体   繁体   English

如何将一个类添加到另一个具有一对多关系的类

[英]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.

相关问题 ASP.Net MVC 如何在ApplicationUser和我自己的类之间建立一对多的关系? - ASP.Net MVC How to create a one-to-many relationship between the ApplicationUser and my own class? 如何在Entity Framework中使用中介包装器类配置一对多关系 - How to configure one-to-many relationship with intermediary wrapper class in Entity Framework 如何以一对多关系向实体添加评论 - How to add a Comment to an entity in a one-to-many relationship EF Core-如何为已经存在一对多关系的同一实体设置多对多 - EF Core - How to setup many-to-many for same entity that already has a one-to-many relationship 一对多和一对多关系 - One-to-many and one-to-many relationship 如何准备与 dapper 的一对多关系 - how to prepare one-to-many relationship with dapper 如何在EF中配置一对多关系 - How to configure a One-to-Many relationship in EF 如何定义聚合的一对多关系 - How to define aggregate one-to-many relationship 如何映射一对多nhibernate类映射 - How to Map One-To-Many nhibernate class mapping 向通用管理类注册抽象托管类,并保留一对多关系 - Register abstract managed classes with generic managing class and preserve one-to-many relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM