实体框架5 MAXLENGTH [英] entity framework 5 MaxLength

查看:211
本文介绍了实体框架5 MAXLENGTH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用EF4和一块code的我发现,从这样一个实体获得最大长度值:

I was using EF4 and a piece of code I found to get the MaxLength value from an entity like this:

public static int? GetMaxLength(string entityTypeName, string columnName)
        {
            int? result = null;
            using (fooEntities context = new fooEntities())
            {
                Type entType = Type.GetType(entityTypeName);
                var q = from meta in context.MetadataWorkspace.GetItems(DataSpace.CSpace)
                                  .Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
                        from p in (meta as EntityType).Properties
                        .Where(p => p.Name == columnName
                                    && p.TypeUsage.EdmType.Name == "String")
                        select p;

                var queryResult = q.Where(p =>
                {
                    bool match = p.DeclaringType.Name == entityTypeName;
                    if (!match && entType != null)
                    {
                        match = entType.Name == p.DeclaringType.Name;
                    }

                    return match;

                }).Select(sel => sel.TypeUsage.Facets["MaxLength"].Value);
                if (queryResult.Any())
                {
                    result = Convert.ToInt32(queryResult.First());
                }

                return result;
            }
        }

不过,我升级到EF5,我知道得到这个错误信息:

However, I upgraded to EF5 and I know get this error message:

...fooEntities'  does not contain a definition for 'MetadataWorkspace' and no
extension method 'MetadataWorkspace' accepting a first argument of type
'...fooEntities' could be found (are you missing a using directive or an assembly
 reference?)

什么是从EF5获取元数据的最佳方式?

What's the best way to get that meta data from EF5?

推荐答案

这意味着你不仅升级EF但是你也改变了API。有两个API - 核心ObjectContext的API和简化的DbContext API。您code是依赖于ObjectContext的API,但EF5使用的DbContext API(因为EF4.1单独EntityFramework.dll组装加)(仅在EF4可用的API)。如果你要使用新的EF功能和您的previous code你应该升级到.NET 4.5。

It means that you have not only upgraded EF but you have also changes the API. There are two APIs - the core ObjectContext API and simplified DbContext API. Your code is dependent on ObjectContext API (the only API available in EF4) but EF5 uses DbContext API (added in separate EntityFramework.dll assembly since EF4.1). If you want to use new EF features and your previous code you should just upgrade to .NET 4.5.

如果你也想用一个新的API,你将不得不更新了很多现有的code,但它仍然有可能获得的ObjectContext 的DbContext ,再次让你的方法工作。你只需要使用这个片段:

If you also want to use a new API you will have to update a lot of your existing code but it is still possible to get ObjectContext from DbContext and make your method work again. You just need to use this snippet:

var objectContext = ((IObjectContextAdapter)context).ObjectContext;

和使用的ObjectContext 而不是在code背景

这篇关于实体框架5 MAXLENGTH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆