简体   繁体   中英

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. 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.

I have other text fields that seem to retain their formatting, but they were inserted using old 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().

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.

You don't show what Error is defined as, so it's a bit hard to see what's going on.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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