Subsonic 3.0.0.4 ActiveRecord模板,用于插入新记录时出现MySQL错误

时间:2010-11-07 14:37:00

标签: c# asp.net mysql subsonic subsonic3

public void Add(IDataProvider provider){


    var key=KeyValue();
    if(key==null){
        var newKey=_repo.Add(this,provider);
        this.SetKeyValue(newKey);
    }else{
        _repo.Add(this,provider); //NullReferenceException was unhandled by user code
    }
    SetIsNew(false);
    OnSaved();
}

''包含

this
{Geocine}
    _age: null
    _birthday: null
    _church_id: null
    _db: {Seminary.Data.SeminaryDB}
    _dirtyColumns: Count = 0
    _finalgrade: null
    _finalgrade_equivalent: null
    _first_name: "Geocine"
    _gender: null
    _isLoaded: false
    _isNew: true
    _last_name: "Cruz"
    _level: null
    _middle_name: "Reilly"
    _repo: {SubSonic.Repository.SubSonicRepository<Seminary.Data.student>}
    _semester_enrolled: null
    _seminary_id: null
    _student_id: 0
    _year_enrolled: null
    age: null
    birthday: null
    church_id: null
    Columns: Count = 14
    finalgrade: null
    finalgrade_equivalent: null
    first_name: "Geocine"
    gender: null
    last_name: "Cruz"
    level: null
    middle_name: "Reilly"
    semester_enrolled: null
    seminary_id: null
    student_id: 0
    tbl: {student}
    TestMode: false
    year_enrolled: null

提供商包含

provider
{SubSonic.DataProviders.DbDataProvider}
    [SubSonic.DataProviders.DbDataProvider]: {SubSonic.DataProviders.DbDataProvider}
    Client: MySqlClient
    ConnectionString: "Data Source=localhost;Database=school;User Id=root;Password=123456;Port=3306;"
    CurrentSharedConnection: null
    DbDataProviderName: "MySql.Data.MySqlClient"
    Factory: {MySql.Data.MySqlClient.MySqlClientFactory}
    Log: null
    Name: "MySql.Data.MySqlClient"
    ParameterPrefix: "@"
    Schema: {SubSonic.Schema.DatabaseSchema}
    SchemaGenerator: {SubSonic.SqlGeneration.Schema.MySqlSchema}

这是 stacktrace

[NullReferenceException: Object reference not set to an instance of an object.]
   MySql.Data.MySqlClient.MySqlConnection.get_ServerThread() +6
   MySql.Data.MySqlClient.MySqlConnection.Abort() +54
   MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) +839
   MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +4
   System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10
   SubSonic.DataProviders.DbDataProvider.ExecuteReader(QueryCommand qry) +175
   SubSonic.Repository.SubSonicRepository`1.Add(T item, IDataProvider provider) +165
   Seminary.Data.student.Add(IDataProvider provider) in C:\Users\SomeUser\Documents\Visual Studio 2008\Projects\Seminary\ActiveRecord\ActiveRecord.cs:2490
   Seminary.Data.student.Save(IDataProvider provider) in C:\Users\SomeUser\Documents\Visual Studio 2008\Projects\Seminary\ActiveRecord\ActiveRecord.cs:2505
   Seminary.Data.student.Save() in C:\Users\SomeUser\Documents\Visual Studio 2008\Projects\Seminary\ActiveRecord\ActiveRecord.cs:2499
   Seminary.Default.btnAdd_Click(Object sender, EventArgs e) in C:\Users\SomeUser\Documents\Visual Studio 2008\Projects\Seminary\Default.aspx.cs:34
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

以下是调用错误的代码:

    student studentObject = new student();
    studentObject.first_name = txtFirstName.Text;
    studentObject.middle_name = txtMiddleName.Text;
    studentObject.last_name = txtLastName.Text;
    studentObject.Save();

1 个答案:

答案 0 :(得分:1)

这有点棘手。当前版本的mysql.connector.net会发生此异常。

通常情况下会抛出一个异常,但是连接器在抛出实际异常之前似乎有一个NullReferenceException。

“真正的”异常可以是“DUPLICATE KEY ...”到“Field XYZ没有默认值”或“你的SQL语法附近有错误......”

我还没有时间弄清楚为什么会发生这种情况(调试Connector.Net或尝试更新的)但同时我会建议你按照我的方式执行以下操作:

下载windows的unix工具:http://unxutils.sourceforge.net/并将它们放在路径中(我们需要tail命令) 配置mysql将query.log写入c:\ temp

[mysqld]
log=c:/temp/query.log

启动控制台并输入

tail -f c:\temp\query.log

现在,您将看到针对本地数据库执行的每个语句,如果您获得NullReferenceException,则将控制台窗口中的最后一个语句复制到mysql查询浏览器实例并在那里执行。

希望有帮助,如果我弄清楚如何解决这个问题,我会更新这篇文章。