Manipulating an entity before saving to DB 在保存到数据库之前操作实体

huangapple go评论75阅读模式
英文:

Manipulating an entity before saving to DB

问题

I understand your code translation request. Here's the translated code:

我在这里有一个理解问题。我试图操作正在创建的实体的字段,但它的工作方式不如预期。

简而言之,我试图更改具有电子邮件的字段,将它们拆分,然后添加一个 `;` 分隔符以区分不同的电子邮件,然后保存到数据库。

这是我的代码:

public async Task<IActionResult> OnPost(Models.Requests.Request newRequest)
{
    newRequest.RequestStatusID = (int)RequestStatusEnum.Requested;
    newRequest.CreatedBy = User.Identity.Name;
    newRequest.CreatedDate = DateTime.Now;
    newRequest.LastRequestDate = DateTime.Now;

    if (!String.IsNullOrEmpty(newRequest.CCRecipients))
    {
        string[] emailSeparators;
        string ccRecipients = "";
        emailSeparators = newRequest.CCRecipients.Split(new Char[] { ' ', ',', ';' });

        foreach (var email in emailSeparators)
        {
            //ccRecipients.Concat(email + "; ");
            
            // 我尝试了两种不同的方法来连接字符串
            ccRecipients = email.Trim() + "; ";
        }

        // 这是我尝试将字符串操纵到字段的地方
        // 据我理解,这是可能的,但我可能错了
        newRequest.CCRecipients = ccRecipients;
    }

    await _changeControlContext.AddAsync(newRequest);
    await _changeControlContext.SaveChangesAsync();

    // 正在执行的其他代码

    return new RedirectToPageResult("RedirectsToAnotherPage");
}

请注意,这只是代码的翻译部分,没有其他内容。如果您有其他问题,请随时提出。

英文:

I have an issue understanding something here. I'm trying to manipulate a field from an entity that is being created, but it is not working as expected.

In short I'm trying to change a field that has emails by splitting them and then adding a ; separator to distinguish the different emails before saving to the database.

This is my code:

public async Task&lt;IActionResult&gt; OnPost(Models.Requests.Request newRequest)
{
    newRequest.RequestStatusID = (int)RequestStatusEnum.Requested;
    newRequest.CreatedBy = User.Identity.Name;
    newRequest.CreatedDate = DateTime.Now;
    newRequest.LastRequestDate = DateTime.Now;

    if (!String.IsNullOrEmpty(newRequest.CCRecipients))
    {
        string[] emailSeparators;
        string ccRecipients = &quot;&quot;;
        emailSeparators = newRequest.CCRecipients.Split(new Char[] { &#39; &#39;, &#39;,&#39;, &#39;;&#39; });

        foreach (var email in emailSeparators)
        {
            //ccRecipients.Concat(email + &quot;; &quot;);
            
            // I have tried two different ways to concatenate the string
            ccRecipients = email.Trim() + &quot;; &quot;;
        }

        // This is where I&#39;m trying to manipulate the string into the field
        // To my understanding this is possible but I could be wrong
        newRequest.CCRecipients = ccRecipients;
    }

    await _changeControlContext.AddAsync(newRequest);
    await _changeControlContext.SaveChangesAsync();

    // Other code that is being done

    return new RedirectToPageResult(&quot;RedirectsToAnotherPage&quot;);
}

There's probably a better way to handle this but for now I want to understand why this is not working or what am I over looking.

In theory it should work but it is not. What could be the issue here?

EDIT: when I mean not working i mean that the values of CCRecipients field of said request are not being save in the database.

For example: RandomEmail@test.com; RandomEmail2@test.com. But in the database, nothing appears is stored. It's a white space instead of null. Sorry for the lack of clarity

答案1

得分: 1

将现有的代码行

ccRecipients = email.Trim() + &quot;; &quot;;

替换为

ccRecipients = ccRecipients + email.Trim() + &quot;; &quot;;

或者使用更好的方法(由@Masoud Andalibi提到)

ccRecipients += email.Trim() + &quot;; &quot;;

英文:

Replace the existing line of code

ccRecipients = email.Trim() + &quot;; &quot;;

with

ccRecipients = ccRecipients + email.Trim() + &quot;; &quot;;

OR with a better approach (as mentioned by @Masoud Andalibi)

ccRecipients += email.Trim() + &quot;; &quot;;

huangapple
  • 本文由 发表于 2023年8月5日 06:44:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定