2014-01-09 40 views
0

在MVC 4數據庫的包管理器中發生錯誤connect - create on on not have primary keys defined。關鍵引用不會激活

error: \tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Event_Class' has no key defined. Define the key for this EntityType.

發現的[Key]挑戰頂級元素是一個關鍵,但無論我做什麼,我不能得到的[Key]這個元素引用。它只是黑色的,編譯器不知道。我是否缺少特定參考或使用?

namespace eManager.Core 
{ 
    public class Event_Class 

    { 
     [Key] 
     public virtual int ClassID { get; set; } 
     public virtual string ClassName { get; set; } 
     public virtual ICollection<Compeditor> Type { get; set; } 


    } 
} 

當前使用:

using eManager; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

回答

0

您可以使用FluentAPI在你EdmEntity類重寫OnModelCreating方法是這樣的:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    builder.Entity<Event_Class>().HasKey(c => c.ClassID); 
} 

然後運行您的遷移和更新數據庫。

+0

謝謝..我剛剛得到它。添加了引用並使用System.ComponentModel.DataAnnotations;它現在還活着。 ! –