title: Silverlight RIA EF6 Code First SQL script generates unknown Discriminator field
body: <p>This scenario is taking place in a complex Silverlight RIA LOB application
  We are upgrading the application from </p>
  
  <pre><code>EntityFramework 4 ==&gt; EntityFramework 6
  Silverlight RIA ==&gt; Open RIA
  Silverlight 4 ==&gt; Silverlight 5
  EF Database First ==&gt; EF Code First
  </code></pre>
  
  <p>We have a Code First EF6 Context class like following</p>
  
  <pre><code>public DbContext()
       : base("DefaultConnection")
  {
     ...
     public DbSet&lt;Parameter&gt; Parameters{ get; set; }
     public DbSet&lt;ParameterType&gt; ParameterTypes { get; set; }
  }
  
  
  [Table("Parameters")]
  public  partial class Parameter
  {
      [Key]
      public int ParameterId {get;set;}
  
      [InverseProperty("ParameterType")]
      public int ParameterTypeId {get;set;}
  
      [ForeignKey("ParameterTypeId")]
      public virtual ParameterType ParameterType { get; set; }
  
      public int? StoreId {get;set;}
  
        ...
  
  }
  
  [Table("ParameterTypes")]
  public partial class ParameterType 
  {
      [Key]
      public global::System.Int32 ParameterTypeId {get;set;}
  
      public global::System.String ParameterName {get;set;}
  
      ...
  
  }
  </code></pre>
  
  <p>Everything compiles and runs well till it hits a statement</p>
  
  <pre><code>var t = Context.Parameters.ToList();
  </code></pre>
  
  <p>the exception states </p>
  
  <blockquote>
    <p>"Invalid column name 'Discriminator'."</p>
  </blockquote>
  
  <p>None of the Table fields has name "Discriminator"
  When I looked at the generated SQL Query it shows</p>
  
  <pre><code>{SELECT 
   CASE WHEN ([Extent1].[Discriminator] = N'Parameter') THEN '0X' WHEN     ([Extent1].[Discriminator] = N'ImportParameters') THEN '0X0X' WHEN ([Extent1].[Discriminator] = N'RecalcParameters') THEN '0X0X0X' WHEN ([Extent1].[Discriminator] = N'RolloverParameters') THEN '0X0X1X' WHEN ([Extent1].[Discriminator] = N'MassChangeParameters') THEN '0X1X' ELSE '0X2X' END AS [C1], 
      [Extent1].[ParameterId] AS [ParameterId], 
      [Extent1].[ParameterTypeId] AS [ParameterTypeId], 
      [Extent1].[StoreId] AS [StoreId], 
      [Extent1].[FiscalYear] AS [FiscalYear], 
      [Extent1].[ParameterEffectiveDate] AS [ParameterEffectiveDate], 
      [Extent1].[ParameterValue] AS [ParameterValue], 
      [Extent1].[UpdatedOn] AS [UpdatedOn], 
      [Extent1].[UpdatedBy] AS [UpdatedBy], 
  CASE WHEN ([Extent1].[Discriminator] = N'Parameter') THEN CAST(NULL AS varchar(1)) WHEN ([Extent1].[Discriminator] = N'ImportParameters') THEN [Extent1].[Prompt] WHEN ([Extent1].[Discriminator] = N'RecalcParameters') THEN [Extent1].[Prompt] WHEN ([Extent1].[Discriminator] = N'RolloverParameters') THEN [Extent1].[Prompt] WHEN ([Extent1].[Discriminator] = N'MassChangeParameters') THEN CAST(NULL AS varchar(1)) END AS [C2], 
  
      ...
  </code></pre>
  
  <p>I have no idea where the Discriminator is coming from , but on the other hand why it checks it against strings like <code>N'RecalcParameters'</code> <code>N'RolloverParameters'</code> and so on.</p>
  
  <p>Upon further investigation I realized that my Parameter class
  has another partial declaration like</p>
  
  <pre><code>[KnownType(typeof(ImportParameters))]
  [KnownType(typeof(RolloverParameters))]
  [KnownType(typeof(RecalcParameters))]
  [KnownType(typeof(MassChangeParameters))]
  [KnownType(typeof(ReforecastParameters))]
  public partial class Parameter
  {
      ...
  }
  </code></pre>
  
  <p>It helps to resolve complex entities inheritance on the Silverlight client side. 
  My Wild guess is that somehow "KnownType" attribute affects the SQL Server script generator in some way. 
  I searched all over the internet and wasn't able to find any clue that could help me resolving this issue. </p>