我正在尝试弄清楚如何使 MVC 脚手架与复合/复杂键一起使用。
我有下表:
public class Data
{
[Key, Column(Order = 0)]
[ForeignKey("Note")]
[Display(Name = "Note id")]
public int NoteId { get; set; }
[Key, Column(Order = 1)]
[ForeignKey("Member")]
[Display(Name = "Member id")]
public int MemberId { get; set; }
[Display(Name = "Description")]
public string Description { get; set; }
[Display(Name = "Note")]
public virtual Note Note { get; set; }
[Display(Name = "Member")]
public virtual Member Member { get; set; }
}
当我执行脚手架行时:
Scaffold Controller Data -Repository
我收到以下错误:
Get-PrimaryKey : Cannot find primary key property for type
Pro.Web.Models.Data'. Multiple properties appear to be
primary keys: NoteId, MemberId
这个问题的解决方案是什么?我使用 Visual Studio 2012。
谢谢。
最佳答案
T4Scaffolding.Core.PrimaryKeyLocators
命名空间下的类 PrimaryKeyLocation
具有在 PrimaryKeyLocation.cs 文件上实现的 IPrimaryKeyLocator
接口(interface)列表本身。
阅读可用的五个实现,我们可以看出您的代码将落在 KeyAttributePropertyLocator
实现上,返回用 [Key] 属性标记的两个成员,但 GetPrimaryKeyCmdlet.cs
从 T4 引擎运行并调用 PrimaryKeyLocation
类具有以下实现:
switch (primaryKeyProperties.Count)
{
case 0:
// Code when no key is found
case 1:
// Code when one key is found
default:
// Code when more than one key is found
WriteError(string.Format("Cannot find primary key property for type '{0}'.
Multiple properties appear to be primary keys: {1}",
foundClass.FullName, primaryKeyPropertyNames));
}
因此,由于 switch 语句不处理多个键,因此不支持复合键。解决这个问题的一种方法是实现复合键的情况,但我不知道这对 t4 模板本身的影响。
关于asp.net-mvc - EF 4.5 Code First 复合键的 MVC 脚手架错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15634609/