实体类型的属性是键的一部分,因此无法进行修改或将其标记为已修改 [英] The property on entity type is part of a key and so cannot be modified or marked as modified

查看:296
本文介绍了实体类型的属性是键的一部分,因此无法进行修改或将其标记为已修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Entity Framework 7.0.0-rc1-final与SQL 2014 LocalDB结合使用(代码优先).我有模型课:

I use Entity Framework 7.0.0-rc1-final with SQL 2014 LocalDB (code first). I have the model class:

public class UnitGroup {
    public int UnitGroupId { get; private set; }
    public string Name { get; set; }

    public ObservableCollection<Unit> UnitSet { get; set; }
    public UnitGroup() {
        UnitSet = new ObservableCollection<Unit>();
    }
}

我使用此委托进行流畅的配置:

I use this delegate for fluent configuration:

Action<EntityTypeBuilder<UnitGroup>> UnitGroupConfig = delegate (EntityTypeBuilder<UnitGroup> e) {
        e.Property(p => p.Name).IsVarchar(25).IsRequired();

        e.HasAlternateKey(p => p.Name);
    };

IsVarchar()是我的扩展方法:

IsVarchar() is my extension method:

public static PropertyBuilder IsVarchar(this PropertyBuilder<string> propertyBuilder, int maxLength) {
        return propertyBuilder.HasColumnType($"varchar({maxLength})");
    }

然后我像这样使用它:

protected override void OnModelCreating(ModelBuilder modelBuilder) {    
        modelBuilder.Entity<UnitGroup>(e => UnitGroupConfig(e));
}

这是此表的迁移代码:

migrationBuilder.CreateTable(
            name: "UnitGroup",
            columns: table => new
            {
                UnitGroupId = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                Name = table.Column<string>(type: "varchar(25)", nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_UnitGroup", x => x.UnitGroupId);
                table.UniqueConstraint("AK_UnitGroup_Name", x => x.Name);
            });

迁移后,在我的Db中有一个表: UnitGroup表

After migration a have table in my Db:UnitGroup table

我需要将EF封装在我的棱镜数据模块中.我有一堂课:

I need to encapsulate EF in my Prism Data Module. I have a class for this:

public class DataService : IDataService {
private DataContext context;

public DataService() {
    context = new DataContext();
}

public IQueryable<T> GetAsTracking<T>(params Expression<Func<T, object>>[] includes) where T : class {
    return includes.Aggregate(context.Set<T>().AsTracking(), (source, expression) => {
        if (expression.Body is MemberExpression) {
            return source.Include(expression);
        }
        return source;
    });
}

public int SaveChanges() {
    return context.SaveChanges();
}

}

最后,我尝试运行如下代码:

Finally, I try to run code like this:

var ds = new DataService();
var ug = ds.GetAsTracking<UnitGroup>().First();
ug.Name="new value";
ds.SaveChanges();

并收到此错误:

实体类型"UnitGroup"上的属性"Name"是键的一部分,因此无法修改或标记为已修改.

The property 'Name' on entity type 'UnitGroup' is part of a key and so cannot be modified or marked as modified.

我在这里找到了类似的问题.一直在编辑主键时出现问题.我检查了所有描述的零件两次.属性名称不是主键的一部分,而是唯一键的一部分.当我使用EF6时,我在部分迁移类中有代码:

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:

CreateIndex("UnitGroup", "Name", unique: true);

,它创建了相同的唯一密钥,并且我可以编辑Name.为什么现在在EF7中现在不可能了?

which was created the same unique key and I had the ability to edit Name. Why now it's impossible now, in EF7?

推荐答案

感谢罗恩·米勒(Rowan Miller),我解决了这个问题.他说:

I solved the problem thanks to Rowan Miller. He said:

在EF Core中,备用键被设计为用作关系(即将有一个指向它的外键).当前EF不支持更改值.

In EF Core, an alternate key is designed to be used as the target of a relationship (i.e. there will be a foreign key that points to it). Currently EF does not support changing the values.

如果我想要该属性的唯一索引,则必须使用以下代码:

If I want a unique index on the property, then I must use this code:

modelBuilder.Entity<UnitGroup>().HasIndex(u => u.Name).IsUnique();

这篇关于实体类型的属性是键的一部分,因此无法进行修改或将其标记为已修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆