Cri*_*ano 2 asp.net-mvc entity-framework scaffolding composite-key ef-code-first
我试图弄清楚如何使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; }
}
Run Code Online (Sandbox Code Playgroud)
当我执行脚手架的行时:
Scaffold Controller Data -Repository
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Get-PrimaryKey : Cannot find primary key property for type
Pro.Web.Models.Data'. Multiple properties appear to be
primary keys: NoteId, MemberId
Run Code Online (Sandbox Code Playgroud)
什么可以解决这个问题?我使用Visual Studio 2012.
谢谢.
命名空间PrimaryKeyLocation下的类T4Scaffolding.Core.PrimaryKeyLocators具有IPrimaryKeyLocator在PrimaryKeyLocation.cs文件本身上实现的接口列表.
阅读可用的五个实现,可以告诉您的代码将落在KeyAttributePropertyLocator实现上,返回标记为[Key] attriubute的成员,但是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));
}
Run Code Online (Sandbox Code Playgroud)
因此,由于switch语句不处理多个键,因此不支持组合键.解决这个问题的一种方法是实现复合键的情况,但我不知道它对t4模板本身的影响.
| 归档时间: |
|
| 查看次数: |
1841 次 |
| 最近记录: |