title: Can't insert the value NULL into column in EF 6.1.3
body: <p>I created a table in SQL Server Management Studio with two columns, <code>FacilityId</code> is the primary key and not null. After the design, I manually added the first row.</p>
  
  <p>However when I insert the new record into the table programmatically in C# code I get this error:</p>
  
  <blockquote>
    <p>Cannot insert the value NULL into column 'FacilityId', table 'xxxxxxxx'; column does not allow nulls. </p>
  </blockquote>
  
  <p>My table in code:</p>
  
  <pre><code>public class MyURL
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.None)]
      [Column("FacilityId")]
      public int FacilityId { get; set; }
      [Column("Url")]
      public string Url { get; set; }
  }
  </code></pre>
  
  <p>To add new record by the below code:</p>
  
  <pre><code>public void AddNewUrl(int id, string url)
  {
          using (MyUrlContext context = new MyUrlContext())
          {
              context.Database.Connection.ConnectionString = GetConnectionString(context);
              var data = GetNewUrl(id, url);
              SaveContextAfterAddingNewRecord(context, data);
          }
  }
  
  public virtual void SaveContextAfterAddingNewRecord(MyUrlContext context, MyURL data)
  {
          context.MyEndpoints.Add(data);
          context.SaveChanges();
  }
  </code></pre>
  
  <p>And </p>
  
  <pre><code>public class MyUrlContext : DbContext
  {
      public MyUrlContext():base()
      {
          Database.SetInitializer&lt;MyUrlContext&gt;(null);
      }
  
      public DbSet&lt;MyURL&gt; MyEndpoints { get; set; }
  }
  </code></pre>
  
  <p>So what is wrong?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The db/table structure.</p>
  
  <p><a href="https://i.stack.imgur.com/PNIfS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PNIfS.png" alt="facilityid"></a></p>
  
  <p>Also, I do have the value provided.</p>
  
  <p>See the image:</p>
  
  <p><a href="https://i.stack.imgur.com/AAANn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AAANn.png" alt="ex"></a></p>
  
  <p>I am sure that the identity property is off.</p>
  
  <p><a href="https://i.stack.imgur.com/w5V2u.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w5V2u.png" alt="3"></a></p>
  
  <p><strong>EDIT2:</strong></p>
  
  <p>The SS profiler indicated </p>
  
  <pre><code>exec sp_executesql N'INSERT [dbo].[TableName]([Url])
  VALUES (@0)
  SELECT [FacilityID]
  FROM [dbo].[TableName]
  WHERE @@ROWCOUNT &gt; 0 AND [FacilityID] = scope_identity()',N'@0 nvarchar(max) ',@0=N'sdfsdf'
  </code></pre>