【问题标题】:Unable to use a Stored Procedure with my Entity Framework v4 + POCO's :(无法将存储过程与我的 Entity Framework v4 + POCO 一起使用 :(
【发布时间】:2011-03-24 01:23:25
【问题描述】:

我有一个非常简单的 Entity Framework 项目,其中包含 POCO Entities。我已经使用EF Wizard 导入了一个stored procedure。邱尔。

然后我创建了自己的EF Entity,然后我将Add Function Import 映射到Stored Procedure 到我的EF Entity

现在...我不确定如何将我的EF Entity 映射到POCO。因此,我不断收到以下错误:

错误 11007:实体类型“XXXXX”是 未映射。

我不确定如何将此实体映射到POCO。有人可以帮忙吗?

【问题讨论】:

    标签: .net entity-framework stored-procedures entity-framework-4 poco


    【解决方案1】:

    没关系。这就是我最终要做的。

    使用 POCO 的

    即。 .edmxCustom Tool 被删除/没有自动生成的实体等。

    1. 导入函数 :: 手动导入存储过程。

    在你的上下文类中...

    public class SqlServerContext : ObjectContext, IUnitOfWork
    {
        public SqlServerContext(EntityConnection entityConnection, 
                                ILoggingService loggingService)
            : base(entityConnection) { .... }
    
        public ObjectResult<Location> FindLocationsWithinABoundingBox(decimal upperLeftLongitude,
                                                                      decimal upperLeftLatitude,
                                                                      decimal lowerRightLongitude,
                                                                      decimal lowerRightLatitude)
        {
            return base.ExecuteFunction<Location>("FindLocationsWithinABoundingBox",
                                                  new ObjectParameter("UpperLeftLatitude", upperLeftLongitude),
                                                  new ObjectParameter("UpperLeftLongitude", upperLeftLatitude),
                                                  new ObjectParameter("LowerRightLongitude", lowerRightLongitude),
                                                  new ObjectParameter("LowerRightLatitude", lowerRightLatitude));
        }
    }
    

    使用自动生成的实体等(EF的默认设置方式)

    1. 创建自定义 COMPLEX TYPE(也将用于映射存储过程)。
    2. 导入函数 :: 手动导入存储过程。
    3. 将 RETURN TYPE(导入的 sp)映射到您创建的自定义 COMPLEX TYPE。

    就是这样。

    不确定我的 POCO 方式是否是做事的最佳方式,但它 .. 很好 .. 有效 :)

    还有this is a related StackOverflow question I asked 关于以服务/IQueryable 方式使用此存储过程... :)

    【讨论】:

    • 我也面临同样的问题。您是如何将返回类型映射到自定义复杂类型的?你能提供一个例子吗?提前致谢
    • 评论有点老了,但您是否意识到在上面的代码中您已经得到:“UpperLeftLatitude” => upperLeftLongitude,反之亦然?即纬度映射到经度?
    • 哈哈哈 :) 很好的发现 :) 不 :( 我没有
    猜你喜欢
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 2020-03-05
    相关资源
    最近更新 更多