Entity Framework 6 - Getting the field size

c# entity-framework entity-framework-6

Question

I need to grab the field size of a column in a model using Entity Framework 6 (the model was defined in the database and the field size is not known at run-time).

I see some sample code for EF4/5 here:

entity framework 5 MaxLength

But unfortunately, this code is broken in EF6:

 var test = objectContext.MetadataWorkspace.GetItems(DataSpace.CSpace);

        if(test == null)
            return null;

        var q = test
            .Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
            .SelectMany(meta => ((EntityType) meta).Properties
            .Where(p => p.Name == columnName && p.TypeUsage.EdmType.Name == "String"));

Reason for this being that objectContext.MetadataWorkspace.GetItems no longer seems to return the same collection type as before, so you can't just search through the collection set in the same way using the 'where' method etc.

Has anybody any ideas of how to tackle this problem in EF6?

1
0
5/23/2017 11:45:13 AM

Popular Answer

Problem was down to my using/includes. These are required:

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Metadata.Edm;
using System.Linq.Expressions;
using System.Linq;
using System;
0
2/2/2016 11:41:37 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow