Calling scalar function from c# using Entity Framework 4.0 / .edmx

前端 未结 5 1371
迷失自我
迷失自我 2020-12-28 15:39

I would like to map my scalar function to my .edmx but it fails. I right click on my entity framework mapping, and choose update model from database. It appears in my stored

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-28 16:15

    I've encountered same problem. And here is solution I've found my self suitable enough (tested in EF5, but should also work in EF4):

    There is no support of mapping scalar-value functions out of the box but you can execute them directly.

    You can also edit edmx file to make edmx generate proper method for scalar-value function, but it ll be deleted if you ll synch you model with database.

    Write scalar-valued function implementation yourself:

    string sqlQuery = "SELECT [dbo].[CountMeals] ({0})";
    Object[] parameters = { 1 };
    int activityCount = db.Database.SqlQuery(sqlQuery, parameters).FirstOrDefault();
    

    Or edit edmx and add Xml for custom maping of scalar-valued function:

    
        
            SELECT [dbo].[CountActivities] (@personId)
        
        
    
    

    This information was found in this blog post

提交回复
热议问题