我试图弄清楚如何使MVC Scaffolding与复合/复合键一起使用。
我有下表:
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。
感谢。
答案 0 :(得分:5)
PrimaryKeyLocation
命名空间下的T4Scaffolding.Core.PrimaryKeyLocators
类有一个IPrimaryKeyLocator
接口列表,这些接口在PrimaryKeyLocation.cs文件本身上实现。
阅读可用的五个实现,可以告诉您的代码将落在KeyAttributePropertyLocator
实现上,返回标记为[Key] attriubute的成员,但是从T4引擎运行的GetPrimaryKeyCmdlet.cs
并调用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模板本身的影响。