我在 C# 中学习 ORM。当我将寄存器添加到寄存器表时,会显示错误。我的课是。
using System;
using System.Collections.Generic;
using System.Linq;using System.Text;
namespace PreSchool
{
public class Child
{
public int ChildId {get; set;}
public string Name { get; set; }
public string Surname { get; set; }
public virtual List<Register> Registers { get; set; }
public Child()
{
this.Registers = new List<Register>();
}
}
和
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace PreSchool
{
public class Register //partial
{
public int RegisterId { get; set; }
public DateTime dt_in { get; set; }
public DateTime dt_out { get; set; }
public virtual Child Child { get; set; }
}
}
功能
private void button1_Click(object sender, RoutedEventArgs e)
{
using (var db = new ChildContext())
{
Child bla= ChildrenGrid.SelectedItem as Child ;
var reg = new Register() { dt_in = DateTime.Now, dt_out = DateTime.Now };
bla.Registers.Add(reg);
db.SaveChanges();
}
}
早些时候我遇到了一个问题,我不得不在注册类中添加“部分”来编译项目,但现在它不是必需的(为什么?)。来自主题的错误显示在这个地方:bla.Registers.Add(reg);
谢谢你的帮助 !