title: The property on entity type is part of a key and so cannot be modified or marked as modified
body: <p>I use Entity Framework 7.0.0-rc1-final with SQL 2014 LocalDB (code first). I have the model class:</p>
  
  <pre><code>public class UnitGroup {
      public int UnitGroupId { get; private set; }
      public string Name { get; set; }
  
      public ObservableCollection&lt;Unit&gt; UnitSet { get; set; }
      public UnitGroup() {
          UnitSet = new ObservableCollection&lt;Unit&gt;();
      }
  }
  </code></pre>
  
  <p>I use this delegate for fluent configuration:</p>
  
  <pre><code>Action&lt;EntityTypeBuilder&lt;UnitGroup&gt;&gt; UnitGroupConfig = delegate (EntityTypeBuilder&lt;UnitGroup&gt; e) {
          e.Property(p =&gt; p.Name).IsVarchar(25).IsRequired();
  
          e.HasAlternateKey(p =&gt; p.Name);
      };
  </code></pre>
  
  <p>IsVarchar() is my extension method:</p>
  
  <pre><code>public static PropertyBuilder IsVarchar(this PropertyBuilder&lt;string&gt; propertyBuilder, int maxLength) {
          return propertyBuilder.HasColumnType($"varchar({maxLength})");
      }
  </code></pre>
  
  <p>Then I use it like this:</p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder) {    
          modelBuilder.Entity&lt;UnitGroup&gt;(e =&gt; UnitGroupConfig(e));
  }
  </code></pre>
  
  <p>This is migration code for this table:</p>
  
  <pre><code>migrationBuilder.CreateTable(
              name: "UnitGroup",
              columns: table =&gt; new
              {
                  UnitGroupId = table.Column&lt;int&gt;(nullable: false)
                      .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                  Name = table.Column&lt;string&gt;(type: "varchar(25)", nullable: false)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey("PK_UnitGroup", x =&gt; x.UnitGroupId);
                  table.UniqueConstraint("AK_UnitGroup_Name", x =&gt; x.Name);
              });
  </code></pre>
  
  <p>After migration a have table in my Db:<a href="https://i.stack.imgur.com/2leYA.png" rel="noreferrer">UnitGroup table</a></p>
  
  <p>I need to encapsulate EF in my Prism Data Module. I have a class for this:</p>
  
  <pre><code>public class DataService : IDataService {
  private DataContext context;
  
  public DataService() {
      context = new DataContext();
  }
  
  public IQueryable&lt;T&gt; GetAsTracking&lt;T&gt;(params Expression&lt;Func&lt;T, object&gt;&gt;[] includes) where T : class {
      return includes.Aggregate(context.Set&lt;T&gt;().AsTracking(), (source, expression) =&gt; {
          if (expression.Body is MemberExpression) {
              return source.Include(expression);
          }
          return source;
      });
  }
  
  public int SaveChanges() {
      return context.SaveChanges();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Finally, I try to run code like this:</p>
  
  <pre><code>var ds = new DataService();
  var ug = ds.GetAsTracking&lt;UnitGroup&gt;().First();
  ug.Name="new value";
  ds.SaveChanges();
  </code></pre>
  
  <p>and get this error:</p>
  
  <blockquote>
    <p>The property 'Name' on entity type 'UnitGroup' is part of a key and so
    cannot be modified or marked as modified.</p>
  </blockquote>
  
  <p>I have found similar questions here. It was a problem in editing primary key all the time. I checked all described parts twice. Property Name is not part of primary key, it's part of the unique key.
  When I have used EF6, I had code in partial class of migration:</p>
  
  <pre><code>CreateIndex("UnitGroup", "Name", unique: true);
  </code></pre>
  
  <p>which was created the same unique key and I had the ability to edit Name. Why now it's impossible now, in EF7?</p>