Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7592853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:00:40+00:00 2026-05-30T21:00:40+00:00

I need to do some trimming before saving various fields in our database. We’re

  • 0

I need to do some trimming before saving various fields in our database. We’re deserializing xml from another application into EF entities and then inserting them. There are few fields in the xml that exceed 4000 chars, and instead of using the TEXT data type, we’d like to trim them instead.

My thought was to inspect MetadataWorkspace and DbChangeTracker inside MyDbContext.SaveChanges() to find any nvarchar(4000) entity properties and trim any string values that are longer than 4000. But I have no idea how to approach this. I couldn’t find any relevant documentation. I saw a few related questions, but none went into any detail or provided any code samples.

Here’s what I’ve got so far:

public override int SaveChanges()
{
    //TODO: trim strings longer than 4000 where type is nvarchar(max)
    MetadataWorkspace metadataWorkspace = 
        ((IObjectContextAdapter) this).ObjectContext.MetadataWorkspace;
    ReadOnlyCollection<EdmType> edmTypes = 
        metadataWorkspace.GetItems<EdmType>(DataSpace.OSpace);

    return base.SaveChanges();
}

Solution

Here’s my solution based on @GertArnold’s answer.

// get varchar(max) properties
var entityTypes = metadataWorkspace.GetItems<EntityType>(DataSpace.CSpace);
var properties = entityTypes.SelectMany(type => type.Properties)
    .Where(property => property.TypeUsage.EdmType.Name == "String"
           && property.TypeUsage.Facets["MaxLength"].Value.ToString() == "Max"
           // special case for XML columns
           && property.Name != "Xml")
    .Select(
        property =>
            Type.GetType(property.DeclaringType.FullName)
            .GetProperty(property.Name));

// trim varchar(max) properties > 4000
foreach (var entry in ChangeTracker.Entries())
{
    var entity = entry.Entity;
    var entryProperties = 
            properties.Where(prop => prop.DeclaringType == entity.GetType());
    foreach (var entryProperty in entryProperties)
    {
        var value = 
            ((string) entryProperty.GetValue(entity, null) ?? String.Empty);
        if (value.Length > 4000)
        {
            entryProperty.SetValue(entity, value.Substring(0, 4000), null);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:00:41+00:00Added an answer on May 30, 2026 at 9:00 pm

    You can find the properties by this piece of code:

    var varchars = context.MetadataWorkspace.GetItemCollection(DataSpace.CSpace)
        .Where(gi => gi.BuiltInTypeKind == BuiltInTypeKind.EntityType)
        .Cast<EntityType>()
        .SelectMany(entityType => entityType.Properties
            .Where(edmProperty => edmProperty.TypeUsage.EdmType.Name == "String")
            .Where(edmProperty => (int)(edmProperty.TypeUsage.Facets["MaxLength"]
                .Value) >= 4000))
        .ToList();
    

    The trick is to extract the entity types from the model by BuiltInTypeKind.EntityType and cast these to EntityType to get access to the Properties. An EdmProperty has a property DeclaringType which shows to which entity they belong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an application where we need to de-serialize some data from one stream
Need some help gathering thoughts on this issue. Our team is moving ahead with
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some best practice advice here... Navigation based application. Root view is a UITableView
Need some help from Oracle app developers out there: I have an C#.NET 4.0
We need some consistency in our functional test cases. The best we can do
need some help from a regex jedi master: If I have a string of
Need some help designing a bash script for grepping IP addresses from auth.log and
I need some ideas how to handle the database structure for a site i'm
Need some advice from you experts out there. I've just started with Android programming

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.