简体   繁体   中英

Read Maximum length of Entity Framework field and assign to textbox in Razor View

I want to map each field the user enters data into correlates with a database field that can easily set by reading database table definition and set it manually in razor view by setting maximum or in my view model but I need to make it dynamic if Database administrator change the maximum character limit through SQL server it will automatically update the maximum limit of character in text field. I am trying to read it from Entity framework and set it in razor view. Is this possible to make it dynamic in any way.

And how can I read max length field from razor view.

After further search I figure out how to read maximum length of field from Entity Framework Schema

public void read()
        {
            using (var db = new PortalContext())
            {
                var objectContext = ((IObjectContextAdapter)db).ObjectContext;
                var container = objectContext.MetadataWorkspace.GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace);
                int maxLength = (int)container.EntitySets["Customers"].ElementType.Properties["LastName"].MaxLength;
            }
        }

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