WP7 SQL CE - 转换为日期时间时发生溢出

发布于 2024-12-09 16:07:11 字数 1674 浏览 0 评论 0原文

我的问题似乎与 帖子和这篇文章但似乎都不适合我的情况。

我有一个定义如下的实体(修改为仅显示列和定义):

public partial class Payee
{
    [Column(Storage = "_PayeeId", IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false)]
    public int PayeeId { get; set; }

    [Column(Storage = "_PayeeName", DbType = "NVarChar(20)", CanBeNull = false)]
    public string PayeeName { get; set; }

    [Column(Storage = "_Active")]
    public System.Nullable<bool> Active

    [Column(IsVersion = true)]
    private Binary _version;

    [Association(Storage = "_Transactions", ThisKey = "PayeeId", OtherKey = "PayeeId")]
    public EntitySet<Transaction> Transactions { get; set; }
}

App.xaml.cs 中,当应用程序最初启动时,我将一些示例数据加载到数据库中

Payee pay1 = new Payee { PayeeName = "My Bank" };
App.ViewModel.SavePayee(pay1);

SavePayee 方法如下:

public void SavePayee(Payee payee)
{
    Datacontext.Payees.InsertOnSubmit(payee);
    Datacontext.SubmitChanges();
}

当我从 App.xaml.cs 调用它时创建“示例数据”时,该方法工作正常。现在,当我稍后尝试在应用程序中创建新的收款人时(txtPayee.Text 确实有值):

Payee payee = txtPayee.SelectedItem as Payee;
if (payee == null)
{
    payee = new Payee { PayeeName = txtPayee.Text };
    App.ViewModel.SavePayee(payee);
}

我收到错误:转换为日期时间时发生溢出。

可能是什么导致这个错误?

My issue seems to be the same as this post and this post but neither seem to apply to my situation.

I have an entity defined as the following (modified to show columns and definitions only):

public partial class Payee
{
    [Column(Storage = "_PayeeId", IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false)]
    public int PayeeId { get; set; }

    [Column(Storage = "_PayeeName", DbType = "NVarChar(20)", CanBeNull = false)]
    public string PayeeName { get; set; }

    [Column(Storage = "_Active")]
    public System.Nullable<bool> Active

    [Column(IsVersion = true)]
    private Binary _version;

    [Association(Storage = "_Transactions", ThisKey = "PayeeId", OtherKey = "PayeeId")]
    public EntitySet<Transaction> Transactions { get; set; }
}

From App.xaml.cs, when the app initially starts, I am loading some sample data into the database:

Payee pay1 = new Payee { PayeeName = "My Bank" };
App.ViewModel.SavePayee(pay1);

And the SavePayee method is as follows:

public void SavePayee(Payee payee)
{
    Datacontext.Payees.InsertOnSubmit(payee);
    Datacontext.SubmitChanges();
}

This works fine when I create my 'sample data' when it is called from App.xaml.cs. Now, when I try to create a new Payee later on in the application (txtPayee.Text DOES have a value):

Payee payee = txtPayee.SelectedItem as Payee;
if (payee == null)
{
    payee = new Payee { PayeeName = txtPayee.Text };
    App.ViewModel.SavePayee(payee);
}

I'm given the error: An overflow occurred while converting to datetime.

What could be causing this error?

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

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

发布评论

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

评论(2

够钟 2024-12-16 16:07:11

事实证明,在这种情况下,错误消息是准确的。这是我发现错误的方法。希望它将来可以帮助其他人:

我的实体被毫无问题地插入,实际上,错误消息是正确的。我怎么知道发生了什么事?我用以下代码包装了 save 方法的内容:

System.IO.IsolatedStorage.IsolatedStorageFile iso = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication(); 
using (checkbookDB.Log = new System.IO.StreamWriter(iso.CreateFile("log.txt"))) 
{ 
}

因此,当该方法发生错误时,它会将结果写入文本文件。收到错误消息后,我使用 WP7 SDK 附带的isolatedStorageExplorerTool 从模拟器下载了文件。我在文本文件中找到了什么?

-- CURSOR BASED INSERT [Payees] 
-- [PayeeName] <= [test payee] 
-- [Active] <= [] 

-- AUTOSYNC [_version] <= [System.Byte[]] 
-- AUTOSYNC [PayeeId] <= [13] 
-- CURSOR BASED INSERT [Transactions] 
-- [TransDate] <= [1/1/0001 12:00:00 AM]
-- [TransAmount] <= [0] 
-- [TransType] <= [0] 
-- [RefNum] <= [] 
-- [Memo] <= [] 
-- [PayeeId] <= [0] 
-- [CategoryId] <= [] 
-- [AccountId] <= [1] 
-- [Cleared] <= [False] 

嗯....好的...看起来我的收款人已正确插入,而且我什至有一个新的收款人 ID。但是等等...交易?为什么有事务尝试插入?

Welp:我是 MVVM 新手。过去一个半月我一直在非工作时间使用它,所以我不熟悉最佳实践。在我的 ViewModel 上,我创建了一个名为 account 的公共实体。我使用此变量来跟踪应用程序中选定的帐户,以便在各处显示正确的数据。在我试图从中保存新收款人的页面中,创建页面时,我创建一个交易实体的实例,以将数据映射到屏幕上的控件/从屏幕上的控件映射数据。那么,我在 OnNavigedTo 方法中所做的就是设置,

transaction.Account = App.ViewModel.account 

因此当我要保存交易时,我知道将其保存到哪个帐户。嗯,这实际上是在做什么(我认为),是告诉我的数据上下文嘿!我有一个交易实体,我正在通过向其添加帐户来更改它。因此,当我输入 datacontext.SaveChanges 方法时,它尝试保存一个空的空白事务实体。

为什么这条线在 OnNavigedTo 中位于最上方?我不知道。我将其移至它所属的 Save 方法中。

/doh

Turns out, in this case, the error message was SPOT ON. Here's how I found my error. Hopefully it helps others in the future:

My entity was being inserted without issue, and actually, the error message was correct. How did I figure out what was going on? I wrapped the contents of my save method with this code:

System.IO.IsolatedStorage.IsolatedStorageFile iso = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication(); 
using (checkbookDB.Log = new System.IO.StreamWriter(iso.CreateFile("log.txt"))) 
{ 
}

So when an error would occur in that method, it would write out the results to the text file. After I got the error message, I downloaded the file from the emulator using the IsolatedStorageExplorerTool that came with the WP7 SDK. What did I find in the text file?

-- CURSOR BASED INSERT [Payees] 
-- [PayeeName] <= [test payee] 
-- [Active] <= [] 

-- AUTOSYNC [_version] <= [System.Byte[]] 
-- AUTOSYNC [PayeeId] <= [13] 
-- CURSOR BASED INSERT [Transactions] 
-- [TransDate] <= [1/1/0001 12:00:00 AM]
-- [TransAmount] <= [0] 
-- [TransType] <= [0] 
-- [RefNum] <= [] 
-- [Memo] <= [] 
-- [PayeeId] <= [0] 
-- [CategoryId] <= [] 
-- [AccountId] <= [1] 
-- [Cleared] <= [False] 

Hmm....Ok...Looks like my Payee is inserting properly, and I even have a new PayeeId. But wait...Transaction? Why is there a transaction trying to insert?

Welp: I'm a noob to MVVM. I've been working with it off-hours for the last month and a half, so I'm not familiar with best practices. On my ViewModel, I created a public entity, named account. I used this variable to track the selected account in the application so the correct data is displayed all over the place. In my page that I was attempting to save a new Payee from, when the page is created, I create an instance of a Transaction entity to map data to/from controls on the screen. Well what I was doing, in my OnNavigatedTo method, was setting

transaction.Account = App.ViewModel.account 

So when I would go to save the transaction, I knew which account to save it to. Well what this was really doing (I think), was telling my datacontext that Hey! I have this transaction entity that I am changing by adding an account to it. So when I would enter the datacontext.SaveChanges method, it tried to save an empty, BLANK transaction entity.

Why was this line way up in the OnNavigatedTo? I'm not sure. I moved it down to the Save method, where it belongs.

/doh

如痴如狂 2024-12-16 16:07:11

就我而言,我试图插入未初始化的“System.DateTime”,并且我的数据库字段不可为空。
据我从你的日志中看到,TransDate 是 System.DateTime.MinValue。即使在可为空的日期时间上,它也会给出溢出错误。您必须将其设置为 null,以便驱动程序将其转换为 System.DBNull.Value。

In my case, I was trying to insert an uninitialized "System.DateTime" and my Database field was not nullable.
As far as I can see from your log, TransDate is System.DateTime.MinValue. It would also give an overflow error even on a nullable datetime. You have to set it to null so the driver wil convert it to System.DBNull.Value.

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