如何在实体框架中使用反射为所有实体定义CompositeKey?

问题描述

我正在尝试为从特定接口(IPersistentEntity)继承的某些实体添加CompositeKey(作为PrimaryKey)。

我正在使用反射来获取从该接口继承的类,然后将这些类配置为使用FluentApi使用CompositeKey。

IEnumerable<Type> types = assemblies.SelectMany(a => a.GetExportedTypes())
            .Where(c => c.IsClass && !c.IsAbstract && c.IsPublic && typeof(IPersistentEntity).IsAssignableFrom(c));

modelBuilder.Entity(type).HasKey("Id","Version");
上面的

执行代码将把 Id 作为PrimaryKey,将 Version 作为AlternateKey。 但是我想把它们都当作CompositeKey。

我以前在下面编写代码来定义CompositeKeys,这没关系...但是现在我不知道有什么区别!

modelBuilder.Entity<Favorite>().HasKey(f => new {f.ProfileId,f.ProductId});

更新

我删除了以前的迁移并重建了解决方案,它现在可以正常工作!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...