Using Linq to Sql in C#, is there any way I can automatically truncate too-long data?

前端 未结 3 1929
孤城傲影
孤城傲影 2021-01-14 20:00

So, I\'m importing data from one database into another. There are about 5,000 records (so nothing ridiculous, but not small enough to eyeball). Is there an easy way to autom

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-14 20:42

    Linq2Sql will generate a property like this:

        [Column(Storage="_Name", DbType="NVarChar(50) NOT NULL")]
        public string Name
        {
            get
            {
                return this._Name;
            }
            set
            {
                if ((this._Name != value))
                {
                    this.OnNameChanging(value);
                    this.SendPropertyChanging();
                    this._Name = value;
                    this.SendPropertyChanged("Name");
                    this.OnNameChanged();
                }
            }
        }
    

    See how it calls a function called OnNameChanged? Just create a function with that name to do the truncation and logging:

    void OnNameChanged()
    {
        if (Name.Length > 50)
        {
            Name = Name.Substring(0, 50);
            LogSomehow("Name truncated");
        }
    }
    

提交回复
热议问题