简体   繁体   English

LINQ InsertOnSubmit丢失格式

[英]LINQ InsertOnSubmit loses formatting

I have a table with a nullable field of type text. 我有一个表,其文本类型为可空字段。 When I run an InsertOnSubmit(), it inserts successfully but all formatting is lost. 当我运行InsertInSubmit()时,它会成功插入,但是所有格式都会丢失。 Specifically, I'm storing the stack trace of an error. 具体来说,我正在存储错误的堆栈跟踪。 Each entry has been formatted into its own line and it looks nice, but when I retrieve it from SQL it loses all its crlf4's. 每个条目都已格式化为自己的行,看起来不错,但是当我从SQL检索它时,它会丢失所有的crlf4。

I have other text fields that seem to retain their formatting, but they were inserted using old ADO.NET. 我还有其他文本字段似乎保留了其格式,但是它们是使用旧的ADO.NET插入的。 How can this be remedied? 如何补救?

Code: 码:

string[] stacktrace = Environment.StackTrace.Replace("\r", "").Replace("\n", "").Replace("  at", "\n").Split('\n');
string stack = "";

for (int i = 0; i < stacktrace.Length; i++)
    stack += stacktrace[i].Trim() + "\r\n";

Error err = new Error();
err.Severity = (byte)sev;
err.Product = (byte)prod;
err.Location = stacktrace[0];
err.Title = message;
err.datetime = DateTime.Now;
err.StackTrace = stack;

dc.Errors.InsertOnSubmit(err);
dc.SubmitChanges();

Note that if I debug into the app, err.StackTrace and stack look correct before and after the SubmitChanges(). 请注意,如果我调试该应用程序,则在SubmitChanges()之前和之后err.StackTrace和堆栈看起来都是正确的。

Error definition: 错误定义:

[Column(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int ID // ...
[Column(Storage="_AID", DbType="Int")]
public System.Nullable<int> AID // ...
[Column(Storage="_Severity", DbType="TinyInt NOT NULL")]
public byte Severity
[Column(Storage="_Product", DbType="TinyInt NOT NULL")]
public byte Product
[Column(Storage="_Location", DbType="VarChar(255) NOT NULL", CanBeNull=false)]
public string Location
[Column(Storage="_Title", DbType="VarChar(255) NOT NULL", CanBeNull=false)]
public string Title
[Column(Storage="_Ex", DbType="Text", UpdateCheck=UpdateCheck.Never)]
public string Ex
[Column(Storage="_Notes", DbType="Text", UpdateCheck=UpdateCheck.Never)]
public string Notes
[Column(Storage="_datetime", DbType="DateTime NOT NULL")]
public System.DateTime datetime
[Column(Storage="_InnerException", DbType="Text", UpdateCheck=UpdateCheck.Never)]
public string InnerException
[Column(Storage="_StackTrace", DbType="Text", UpdateCheck=UpdateCheck.Never)]
public string StackTrace

[Association(Name="Activity_Error", Storage="_Activity", ThisKey="AID", IsForeignKey=true)]
public Activity Activity

Are you sure that whatever you are using to view the errors isn't stripping out the whitespace? 您确定用于查看错误的内容不会剥离空白吗? For intance, any web browser will ignore \\r\\n. 例如,任何Web浏览器都将忽略\\ r \\ n。

You don't show what Error is defined as, so it's a bit hard to see what's going on. 您没有显示Error的定义,因此很难知道发生了什么。

EDIT: 编辑:

It looks to me like the problem is how you are viewing the data afterwards. 在我看来,问题在于您随后如何查看数据。 How are you determining that the formatting is wrong? 您如何确定格式错误?

Did you actually try to look into your SQL database by using the Studio Manager and see what is actually stored in the database. 您是否真的尝试过使用Studio Manager查看SQL数据库并查看数据库中实际存储的内容。 By looking at the data there, you know if there is a problem STORING the data or RETRIEVING the data. 通过查看那里的数据,您可以知道存储数据或检索数据是否存在问题。

I think the problem lies in the retrieval of the data. 我认为问题在于数据的检索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM