-
October 9th, 2013, 02:34 AM
#1
Entity Framework - Get Max Length of Column
I've been looking for at way to ask EF about the max length of columns to avoid truncation errors.
I found these two articles:
http://blog.chronoclast.com/2010/10/...ld-length.html
and
http://stackoverflow.com/questions/7...tity-framework
but I can't get it to work for me. I'm not sure where to get the Context that they talk about.
Or is there another way?
-
October 9th, 2013, 02:45 AM
#2
Re: Entity Framework - Get Max Length of Column
Context is the DbContext or ObjectContext.
How are you using EF? CodeFirst or Designer? Post a code snippet of using your approach to making an EF call (then we can point out the context).
-
October 9th, 2013, 02:47 AM
#3
Re: Entity Framework - Get Max Length of Column
Code:
public bool VerifyOrderNumber(int OrderNumber)
{
Log4netProperties.AddOriginatingServiceMethodToLog4netProperties();
try
{
return entityFramework.RepairOrder.Where(r => r.RepairOrderID == OrderNumber).Count()>0;
}
catch (Exception ex)
{
log.Error(ex);
entityFramework = new TCXMLIEntities();
return false;
}
}
Here's a snippet from my code
Thanks for your fast reply :-)
-
October 9th, 2013, 02:48 AM
#4
Re: Entity Framework - Get Max Length of Column
Where
entityFramework = new TCXMLIEntities();
and
public partial class TCXMLIEntities : ObjectContext
{....}
-
October 9th, 2013, 02:49 AM
#5
Re: Entity Framework - Get Max Length of Column
entityFramework is your context. What does this variable's class derive from?
-
October 9th, 2013, 02:50 AM
#6
Re: Entity Framework - Get Max Length of Column
Okay. Posting more code while I was answering. Yep, entityFramework is the context.
-
October 9th, 2013, 02:52 AM
#7
Re: Entity Framework - Get Max Length of Column
I tried to add a function looking like this
Code:
public int Length<TEntity>(string field)
{
var Context = ((IObjectContextAdapter)entityFramework).ObjectContext;
var item = Context.MetadataWorkspace.GetItem<EntityType>(typeof(TEntity).FullName, DataSpace.CSpace);
return (int)item.Properties[field].TypeUsage.Facets["MaxLength"].Value;
}
but it is not pleased with that cast (to IObjectContextAdapter)
-
October 9th, 2013, 03:07 AM
#8
Re: Entity Framework - Get Max Length of Column
-
October 9th, 2013, 03:22 AM
#9
Re: Entity Framework - Get Max Length of Column
Okay...this was weird...
I made this hack to my code:
Code:
public int Length<TEntity>(string field)
{
var item = /*Context*/entityFramework.MetadataWorkspace.GetItem<EntityType>(typeof(TEntity).FullName.Replace("TCXMLI_DAL", "EfTCXMLI"), DataSpace.CSpace);
return (int)item.Properties[field].TypeUsage.Facets["MaxLength"].Value;
}
Because it claimed that the class repairorder didn't exist in the metadata<something>.
the class looks like this in the generated code:
Code:
namespace TCXMLI_DAL
{
....
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmEntityTypeAttribute(NamespaceName="EfTCXMLI", Name="RepairOrder")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class RepairOrder : EntityObject
{....}
....
}
But things appear to work now (or: at least I got something useful back and not a crash!)
Thank you all for your replies. :-)
I don't know how the namespace mismatch happened, but at least now I can continue :-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|