如何在 NHibernate 中插入引用外键的子元素?
我对 Fluent NHibernate 还很陌生。不知道我应该如何处理这个问题。
我有两个实体:
public class Student
{
public virtual Guid StudentId { get; set; }
public virtual String Dept_id { get; set; }
public virtual String Name { get; set; }
public virtual int Age { get; set; }
public virtual String Address { get; set; }
public virtual Department Department { get; set; }
}
public class Department
{
public virtual int Dept_id { get; set; }
public virtual String Dept_name { get; set; }
public virtual IList<Student> Students { get; set; }
public Department()
{
Students = new List<Student>();
}
}
并且映射是:
public class DepartmentMap : ClassMap<Department>
{
public DepartmentMap()
{
Table("Department");
Id(x => x.Dept_id).Column("Dept_id");
Map(x => x.Dept_name).Column("Dept_name");
HasMany(x => x.Students).KeyColumn("Student_id").Inverse()
.Cascade.All();
}
}
public class StudentMap :ClassMap<Student>
{
public StudentMap()
{
Table("Student");
Id(x => x.StudentId).Column("Student_id").GeneratedBy.GuidComb();
Map(x => x.Name);
Map(x => x.Age);
Map(x => x.Address);
Map(x => x.Dept_id).Column("Dept_id");
References(x => x.Department).Column("Dept_id").Not.Nullable();
}
}
当我尝试插入为:时,
StudentRepository rep = new StudentRepository();
Student s = new Student();
s.Name = txtname.Text;
s.Age = int.Parse(txtage.Text);
s.Address = txtaddress.Text;
s.Dept_id = dddept.SelectedItem.Value;
rep.Add(s);
它给我的错误为:
{"not-null property references a null or transient value nHibernateTest.Domain.Student.Department"}
I am still new to Fluent NHibernate. Not sure how I should approach this.
I have two entities:
public class Student
{
public virtual Guid StudentId { get; set; }
public virtual String Dept_id { get; set; }
public virtual String Name { get; set; }
public virtual int Age { get; set; }
public virtual String Address { get; set; }
public virtual Department Department { get; set; }
}
public class Department
{
public virtual int Dept_id { get; set; }
public virtual String Dept_name { get; set; }
public virtual IList<Student> Students { get; set; }
public Department()
{
Students = new List<Student>();
}
}
and the mappings are :
public class DepartmentMap : ClassMap<Department>
{
public DepartmentMap()
{
Table("Department");
Id(x => x.Dept_id).Column("Dept_id");
Map(x => x.Dept_name).Column("Dept_name");
HasMany(x => x.Students).KeyColumn("Student_id").Inverse()
.Cascade.All();
}
}
public class StudentMap :ClassMap<Student>
{
public StudentMap()
{
Table("Student");
Id(x => x.StudentId).Column("Student_id").GeneratedBy.GuidComb();
Map(x => x.Name);
Map(x => x.Age);
Map(x => x.Address);
Map(x => x.Dept_id).Column("Dept_id");
References(x => x.Department).Column("Dept_id").Not.Nullable();
}
}
when i am trying to insert as :
StudentRepository rep = new StudentRepository();
Student s = new Student();
s.Name = txtname.Text;
s.Age = int.Parse(txtage.Text);
s.Address = txtaddress.Text;
s.Dept_id = dddept.SelectedItem.Value;
rep.Add(s);
It's giving me error as:
{"not-null property references a null or transient value nHibernateTest.Domain.Student.Department"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您像帖子中那样这样做,您将收到错误,因为您想发送 Department 对象的空数据。正如科尔·W 所说。您需要将要添加的部门的 ID 与数据库中现有部门的 ID 进行比较。
我将从 Cole W 答案中定义 SelectedDepartment。您可以将其添加到存储库。
If you will be doing this like is in your post you will get an error because you want to send empty data of Department object. As Cole W says. You need to compare id of Department which you want to add with id of existing Department from database.
I will define SelectedDepartment from Cole W answer. You can add it to repository.
当您创建新学生时,您需要设置部门而不仅仅是 ID。您是否有理由为此需要一个单独的财产?我会从您的实体和映射类中删除此属性,然后保留部门引用。如果您需要部门 ID,您可以通过 Student.Department.Dept_id 引用它。
基本上,您正在尝试在外键列中插入具有空值的实体。您需要将您的创作更改为如下所示:
When you are creating your new Student you need to set the Department not just the Id. Is there a reason you need a separate property for this? I would remove this property from your entity and from your mapping class and just leave in the Department reference. If you need the Department Id you can just reference it via Student.Department.Dept_id.
Basically you are trying to insert an entity with a null value in a foreign key column. You would need to change your creation to something like this: