How do you make non foreign key constraints in NHibernate?
I am using fluent nhibernate(v1.2) and nhibernate(v3.1) and I am having a weird Problem.
I have this
public class GradeMap : ClassMap<Grade>
{
public GradeMap()
{
Id(x => x.GradeId);
Map(x => x.TaskName).NvarcharWithMaxSize().Not.Nullable();
Map(x => x.Weight).Not.Nullable().Precision(5).Scale(2);
Map(x => x.OutOf).Not.Nullable().Precision(5).Scale(2);
Map(x => x.Mark).Not.Nullable().Precision(5).Scale(2);
Map(x => x.CourseBackgroundColor).Not.Nullable();
Map(x => x.CoursePrefix).Not.Nullable();
References(x => x.Student).Not.Nullable();
References(x => x.Course);
}
}
public class CourseMap : ClassMap<Course>
{
public CourseMap()
{
Id(x => x.Id).Column("CourseId");
Map(x => x.Prefix).NvarcharWithMaxSize().Not.Nullable();
HasMany(x => x.Tasks).Cascade.Delete().Inverse();
HasMany(x => x.CoursePermissions).Cascade.All().Inverse();
HasMany(x => x.CourseSharing).Cascade.All().Inverse();
HasMany(x => x.Grades).Cascade.None().Inverse();
}
}
I then do something like this
return session.Query<Grade>().ToList();
If I would try to grab a propety such as Grade.Course.Id
it would crash and I would get.
Grade.Course = {Castle.Proxies.CourseProxy}
Grade.Course.Id = '((new System.Collections.Generic.Mscorlib_CollectionDebugView<OnlGrade>(grades)).Items[0].Course).Id' threw an exception of type 'NHibe开发者_如何学编程rnate.ObjectNotFoundException'
Grade.Course.Prefix = above error except .Prefix instead of .Id
I would have thought Course Object would be empty or null. Not that it would have some Proxy with all properties throwing exceptions.
Edit
I found this posting but I don't have this attribute anymore so maybe they got rid of it or moved it.
Anyone know?
Is it possible to avoid NHibernate.ObjectNotFoundException when there is a foreign key but the referenced row does not exist?
I think the error here is because you're accessing the Id property of an object which does not exist.
Do you get the same error if you do TableA.TableB
?
What about NotFound.Ignore()?
public class GradeMap : ClassMap<Grade>
{
public GradeMap()
{
Id(x => x.GradeId);
Map(x => x.TaskName).NvarcharWithMaxSize().Not.Nullable();
Map(x => x.Weight).Not.Nullable().Precision(5).Scale(2);
Map(x => x.OutOf).Not.Nullable().Precision(5).Scale(2);
Map(x => x.Mark).Not.Nullable().Precision(5).Scale(2);
Map(x => x.CourseBackgroundColor).Not.Nullable();
Map(x => x.CoursePrefix).Not.Nullable();
References(x => x.Student).Not.Nullable();
References(x => x.Course)
.NotFound.Ignore();
}
}
EDIT: Let's say database schema looks like this:
Grade(GradeId, TaskName, Course_id)
Course(CourseId, Prefix)
If there is no foreign key constraint on Course_id column, then row in Course table with CourseId that corresponds to Course_id column in Grade table can be deleted. E.g.:
Course table:
CourseId Prefix
1 Course1Prefix
Grade table:
GradeId TaskName Course_id
1 Grade1Task 1
without foreign key constraint you can issue this dml query:
delete from Course where CourseId = 1
And it could be reason of problem explained by sJhonny.
When "NotFound.Ignore()" is used in the mapping then NHibernate tries to load Courses that belong to all Grades that were loaded by the query:
session.Query<Grade>().ToList();
No proxies are generated and when course referenced by grade is not found in database then the Course property is simply null.
If you can modify the database schema it would be helpful to create PK constraint on Course_id column.
精彩评论