我可能已经阅读了与此问题相似的每个Stackoverflow帖子,但却无法弄清楚发生了什么。
我必须上课学生和教育背景,学生可以有一个或多个教育背景,使这成为一对多的关系。
映射文件:
Student.hbm.xml
<class name="Student" table="student">
<id name="Id" column="id">
<generator class="guid" />
</id>
<property name="Code" column="code" />
<property name="DateTimeCreated" column="datetime_created" />
<property name="FirstName" column="first_name" />
<property name="MiddleName" column="middle_name" />
<property name="LastName" column="last_name" />
<property name="BirthDate" column="birth_date" />
<property name="HomePhone" column="home_phone" />
<property name="Mobile" column="mobile" />
<property name="HomeAddress" column="home_address" />
<property name="CountryCode" column="country_code" />
<property name="ZipCode" column="zip_code" />
<property name="MotherName" column="mother_name"/>
<property name="MotherAddress" column="mother_address" />
<property name="MotherContactNo" column="mother_contact_no" />
<property name="FatherName" column="father_name" />
<property name="FatherAddress" column="father_address" />
<property name="FatherContactNo" column="father_contact_no" />
<property name="CreatedById" column="created_by_id" />
<bag name="EducationalBckgrnd" table="student_educ_bckgrnd" inverse="true" cascade="all">
<key column="student_id" />
<one-to-many class="StudentEducBckgrnd" />
</bag>
StudentEducBckgrnd.hbm.xml
<class name="StudentEducBckgrnd" table="student_educ_bckgrnd">
<id name="Id" column="id">
<generator class="guid" />
</id>
<!--<property name="StudentId" column="student_id" type="System.Guid" insert="false" />-->
<property name="SchoolName" column="school_name" />
<property name="Remarks" column="remarks" />
<property name="From" column="from" />
<property name="To" column="to" />
<many-to-one name="EnrolledStudent" class="Student" column="student_id" />
如果我不注释掉上面的StudentId属性,我会得到参数索引错误。
Student.cs
public class Student
{
public virtual IList<StudentEducBckgrnd> EducationalBckgrnd { get; set; }
public virtual Guid Id { get; set; }
public virtual string Code { get; set; }
public virtual DateTime DateTimeCreated { get; set; }
public virtual string FirstName { get; set; }
public virtual string MiddleName { get; set; }
public virtual string LastName { get; set; }
public virtual DateTime BirthDate { get; set; }
public virtual string HomePhone { get; set; }
public virtual string Mobile { get; set; }
public virtual string HomeAddress { get; set; }
public virtual string CountryCode { get; set; }
public virtual string ZipCode { get; set; }
public virtual string MotherName { get; set; }
public virtual string MotherAddress { get; set; }
public virtual string MotherContactNo { get; set; }
public virtual string FatherName { get; set; }
public virtual string FatherAddress { get; set; }
public virtual string FatherContactNo { get; set; }
public virtual Guid CreatedById { get; set; }
public Student()
{
EducationalBckgrnd = new List<StudentEducBckgrnd>();
}
}
StudentEducBckgrnd.cs
public class StudentEducBckgrnd
{
public virtual Guid Id { get; set; }
public virtual Guid StudentId { get; set; }
public virtual string SchoolName { get; set; }
public virtual string From { get; set; }
public virtual string To { get; set; }
public virtual string Remarks { get; set; }
public virtual Student EnrolledStudent { get; set; }
}
如果我使用上述所有代码,则会收到以下错误:
NHibernate.Exceptions.GenericADOException was caught
的HResult = -2146232832 Message =无法插入:[EnrollmentSystem.Domain.StudentEducBckgrnd#6a0fdb25-18ad-4658-b8b3-e62b8d220649] [SQL:INSERT INTO student_educ_bckgrnd(school_name,remarks,from,to,student_id,id)VALUES(?,?,?, ?,?,?)] 来源= NHibernate的 SqlString = INSERT INTO student_educ_bckgrnd(school_name,remarks,from,to,student_id,id)VALUES(?,?,?,?,?,?) 堆栈跟踪: at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id,Object [] fields,Boolean [] notNull,Int32 j,SqlCommandInfo sql,Object obj,ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id,Object [] fields,Object obj,ISessionImplementor session) 在NHibernate.Action.EntityInsertAction.Execute() 在NHibernate.Engine.ActionQueue.Execute(IExecutable可执行文件) 在NHibernate.Engine.ActionQueue.ExecuteActions(IList列表) 在NHibernate.Engine.ActionQueue.ExecuteActions() 在NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource会话) 在NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent事件) 在NHibernate.Impl.SessionImpl.Flush() 在NHibernate.Transaction.AdoTransaction.Commit() 在EnrollmentSystem.Business.CStudent.SaveStudent(学生,List`1 educationList)的d:\ Admiral \ projects \ EnrollmentSystem \ EnrollmentSystem \ Business \ CStudent.cs:第54行 InnerException:MySql.Data.MySqlClient.MySqlException 的HResult = -2147467259 消息=您的SQL语法中有错误;查看与MySQL服务器版本对应的手册,以便在'from,to,student_id,id)附近使用正确的语法VALUES('fdsaf','asdf','2014-01-28','2014-01-28 ',''在第1行 来源= MySql.Data 错误码= -2147467259 数= 1064 堆栈跟踪: at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&amp; affectedRow,Int64&amp; insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId,Int32&amp; affectedRows,Int64&amp; insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId,Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() 在NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd) 在NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation期望) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id,Object [] fields,Boolean [] notNull,Int32 j,SqlCommandInfo sql,Object obj,ISessionImplementor session) InnerException:
我正在使用MySQL而且我正在构建一个C#winforms应用程序,错误告诉我我有一个SQL语法错误,但我无法弄清楚原因。
非常感谢任何帮助。
NEWBIE
答案 0 :(得分:1)
此处的问题是关键字。这种映射
<property name="From" column="from" />
说,列名是来自。但 FROM 是关键词(几乎对于我敢打赌的任何数据库引擎......)。因此,我们需要做的是使用特殊符号来逃避该设置:`
(我不完全确定这个标志,因为在MS SQL Server上我确实使用这种风格:[from]
)
<property name="From" column="`from`" />
同时检查例外
...要在'附近附近使用正确的语法,to,student_id,...
顺便说一下,两次使用的列的正确映射就像这样
<many-to-one name="EnrolledStudent" class="Student" column="student_id" />
<property name="StudentId" type="System.Guid" column="student_id"
insert="false" update="false" />
在这种情况下,INSERT或UPDATE只使用列student_id
一次,参考映射