Subsonic 3.0.0.4 MySQL ActiveRecord 模板插入新记录时出错

发布于 2024-09-30 20:21:02 字数 4032 浏览 0 评论 0 原文

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' contains

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 contains

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();
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' contains

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 contains

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}

Here is the 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

Here is the code that called the error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜还是长夜 2024-10-07 20:21:02

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

通常,此时会抛出异常,但连接器在抛出实际异常之前似乎本身就有一个 NullReferenceException。

“真正的”异常可能是从“重复键...”到“字段 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 查询浏览器实例并在那里执行它。

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

That is a bit tricky. This exceptions happens with the current version of mysql.connector.net.

Normally an exception would be trown at this point, but the connector seems to have an NullReferenceException itself before it can throw the actual exception.

The "real" exception could be anything from "DUPLICATE KEY ..." to "Field XYZ does not have a default value" or "You have an error in your SQL Syntax near ..."

I haven't had time yet to figure out why this happens (debug the Connector.Net or try a newer one) but in the mean time I would suggest you do the following as I do:

Download unix tools for windows: http://unxutils.sourceforge.net/ and put them in the path (we need the tail command)
Configure mysql to write a query.log to c:\temp

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

Start a console and type

tail -f c:\temp\query.log

Now you see every statement that is executed against your local database and if you get the NullReferenceException copy the last statement form the console window to a mysql query browser instance and execute it there.

Hope that helps, I will update this post if I figured out how to fix this issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文