简体   繁体   English

在带有EF6模型的WEB API中,如何限制返回的VARCHAR(MAX)列的长度

[英]In WEB API with EF6 models, how do I limit the length of a VARCHAR(MAX) column returned

I have a user that will not be able to update their client this year. 我有一个用户今年将无法更新其客户端。 One of my columns is VARCHAR(MAX) and their current client chokes on the field when it contains over 250 characters. 我的专栏之一是VARCHAR(MAX),当该字段包含250个以上的字符时,它们当前的客户端阻塞。 Since they are the only authorized client using this endpoint, I'd like to relieve their pain and just truncate the column at 250 characters. 由于他们是使用此终结点的唯一授权客户端,因此我想减轻他们的痛苦,并以250个字符截断该列。 This is a read-only endpoint so I don't have to deal with updates. 这是一个只读端点,因此我不必处理更新。

I was thinking this would be a good place for partial classes - maybe could do this: 我以为这是部分课程的好地方-也许可以这样做:

  public partial class Equipment_Detail
  {   
    public string note
    {
        get
        {
            return left(note, 250);
        }
        set { }
    }
  }

but it conflicts with the existing definition. 但它与现有定义冲突。 I know I'm missing something very fundamental here but I only use .NET once a year. 我知道我在这里遗漏了一些非常基本的东西,但是我每年只使用一次.NET。

DTOs are a great idea but with all the .Include() in my controller (my model is quite complex at this point), I think I found a simpler solution : DTO是一个好主意,但是在我的控制器中包含所有.Include() (此时我的模型非常复杂),我认为我找到了一个更简单的解决方案:

  1. rename the column in the designer from 'note' to 'fullnote' and make it private 将设计器中的列从'note'重命名为'fullnote'并将其设为私有 Visual Studio属性屏幕快照
  2. in the partial class, create a note field and using the private fullnote, truncate if necessary: 在部分类中,创建一个注释字段,并使用私有的完整注释,必要时截断:

     public string note { get { string _note = fullnote; if (!String.IsNullOrEmpty(_note)) { if (_note.Length > 250) { _note = _note.Substring(0, 250); } } return _note; } set { } } 

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

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