title: Entity Framework 6 Code First function mapping
body: <p>I want integrate Entity Framework 6 to our system, but have problem.</p>
  
  <ol>
  <li>I want to use Code First. I don’t want to use Database First *.edmx file for other reasons.</li>
  <li>I use attribute mapping [Table], [Column] and this works fine</li>
  <li>Database has many User-Defined Functions and I need to use them in Linq To Entities query.</li>
  </ol>
  
  <p><strong>Problem is:</strong></p>
  
  <p>I cannot map function via attribute like [Table], [Column]. Only 1 attribute is available [DbFunction], which requires *.edmx file.</p>
  
  <p>I’m ok to have functions mapping in *.edmx file, but it means I cannot use attributes mapping for Entities: [Table], [Column]. Mapping must be full in *.edmx or in attributes.</p>
  
  <p>I tried to create DbModel and add function via this code:</p>
  
  <pre><code>public static class Functions
  {
      [DbFunction("CodeFirstNamespace", "TestEntity")]
      public static string TestEntity()
      {
          throw new NotSupportedException();
      }
  }
  
  
  public class MyContext : DbContext, IDataAccess
  {
      protected MyContext (string connectionString)
          : base(connectionString, CreateModel())
      {
      }
  
      private static DbCompiledModel CreateModel()
      {
          var dbModelBuilder = new DbModelBuilder(DbModelBuilderVersion.Latest);
          dbModelBuilder.Entity&lt;Warehouse&gt;();
          var dbModel = dbModelBuilder.Build(new DbProviderInfo("System.Data.SqlClient", "2008"));
  
          var edmType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String);
          var payload =
              new EdmFunctionPayload
              {
                  Schema = "dbo",
                  ParameterTypeSemantics = ParameterTypeSemantics.AllowImplicitConversion,
                  IsComposable = true,
                  IsNiladic = false,
                  IsBuiltIn = false,
                  IsAggregate = false,
                  IsFromProviderManifest = true,
                  StoreFunctionName = "TestEntity",
                  ReturnParameters =
                      new[]
                      {
                          FunctionParameter.Create("ReturnType", edmType, ParameterMode.ReturnValue)
                      }
              };
  
          var function = EdmFunction.Create("TestEntity", "CodeFirst", DataSpace.CSpace, payload, null);
          dbModel.DatabaseMapping.Model.AddItem(function);
          var compiledModel = dbModel.Compile();       // Error happens here
          return compiledModel;
      }
  }
  </code></pre>
  
  <p>But have exception: </p>
  
  <p>One or more validation errors were detected during model generation:</p>
  
  <pre><code>Edm.String: : The namespace 'String' is a system namespace and cannot be used by other schemas. Choose another namespace name.
  </code></pre>
  
  <p>Problem is in “edmType” variable. I cannot create correctly ReturnType for function.
  Can anybody suggest how I can add function into model?
  Interface of adding function is exposed, so it should be able to do, but there is no information in web for this situation.
  Probably, somebody knows when Entity Framework team is going to implement attribute mapping for functions like Line To Sql does.</p>
  
  <p>EF version: 6.0.0-beta1-20521</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p>Yes, this works for me. But for scalar functions only. I, also, need map function, which returns IQueryable:</p>
  
  <pre><code> IQueryable&lt;T&gt; MyFunction()
  </code></pre>
  
  <p>Where T is EntityType or RowType or any Type. I cannot do this at all (EF version is 6.0.2-21211). I think this should work in this way:</p>
  
  <pre><code>private static void RegisterEdmFunctions(DbModel model)
  {
      var storeModel = model.GetStoreModel();
      var functionReturnValueType = storeModel.EntityTypes.Single(arg =&gt; arg.Name == "MyEntity").GetCollectionType();
      var payload =
          new EdmFunctionPayload
          {
              IsComposable = true,
              Schema = "dbo",
              StoreFunctionName = "MyFunctionName",
              ReturnParameters =
                  new[]
                  { 
                      FunctionParameter.Create("ReturnValue", functionReturnValueType, ParameterMode.ReturnValue)
                  },
              Parameters =
                  new[]
                  {
                      FunctionParameter.Create("MyFunctionInputParameter", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32), ParameterMode.In)
                  }
          };
      storeModel.AddItem(EdmFunction.Create(
          payload.StoreFunctionName,
          "MyFunctionsNamespace",
          DataSpace.SSpace,
          payload,
          payload.Parameters.Select(arg =&gt; MetadataProperty.Create(arg.Name, arg.TypeUsage, null)).ToArray()));
  }
  </code></pre>
  
  <p>But still no luck:</p>
  
  <pre><code>  model.Compile();  // ERROR 
  </code></pre>
  
  <p>Is it possible or not?
  Probably steps are not right?
  Probably support will be added at EF 6.1.
  Any information will be very useful.</p>
  
  <p>Thanks!</p>