1

のためのキーを定義します - databseで私はこのコード行でこのエラーを取得し、このEntityType

ReportRunnerEntities reportDB = new ReportRunnerEntities(); 

    public ActionResult Index() 
    { 
     **var types = reportDB.ReportTypes.ToList();** 
     return View(types); 
    } 

表は、主定義されたキーと設定のアイデンティティを持っています。

私のモデルがある -

namespace ReportRunner.Models 
{ 
    public partial class ReportRunnerEntities : DbContext 
    { 
     public DbSet<Reports> Report { get; set; } 
     public DbSet<ReportTypes> ReportTypes { get; set; } 
     public DbSet<Users> Users { get; set; } 
    } 
} 

namespace ReportRunner.Models 
{ 
    public partial class ReportTypes 
    { 
     public int ReportTypeId { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
     public List<Reports> Reports { get; set; } 
    } 
} 

namespace ReportRunner.Models 
{ 
    public class Reports 
    { 
     public int ReportId { get; set; } 
     public int ReportTypeId { get; set; } 
     public int UserId { get; set; } 
     public string Title { get; set; } 
     public ReportTypes ReportType { get; set; } 
    } 

}

namespace ReportRunner.Models 
{ 
    public partial class Users 
    { 
     public int UserId { get; set; } //ArtistId 
     public string Name { get; set; } 
    } 
} 

、ここでは私の接続文字列である -

私はそれがデータベースに到達したことがないだと思われます。私が言ったように、キーはデータベースに設定されています。

何か不足していますか?

+0

エラーを投稿してください。 – Nix

+0

そして、ReportTypeId、ReportId、およびUserIdがプライマリキーであることをEFはどこで言いますか? –

答えて

2

私は変更する必要があります参照カップルの事があります。

  • ReportTypesは

  • 公共リストレポート{取得ReportType

    する必要があります。 が設定されました。 }は公開されているはずです ICollectionレポート{get; が設定されました。 }

    あなたは何を1、それはこのようなあなたの ReportRunnerEntitiesクラスのコンストラクタを使用して あるEF伝える必要があり
  • あなたのweb.configファイル内 接続文字列を定義している場合は、:


    namespace ReportRunner.Models 
    { 
        public partial class ReportRunnerEntities : DbContext 
        { 
         public ReportRunnerEntities : base("name=NameOfConnectionInWebConfig") 
         {} 
         public DbSet<Reports> Report { get; set; } 
         public DbSet<ReportTypes> ReportTypes { get; set; } 
         public DbSet<Users> Users { get; set; } 
        } 
    } 
    

あなたはここにいるの詳細を読むことができます:http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx

.NET MVCとEF Code Firstをスタックとして使用する予定の場合は、RepositoryとUnit of Workのパターンを使用します。これを設定する方法についての良い記事は次のとおりです。Entity Framework 4 CTP 4/CTP 5 Generic Repository Pattern and Unit Testable

+0

Paulさん、ありがとう、とても助かりました。 – duckmike

関連する問題