2017-04-21 10 views
0

私はデータベースとテーブルに接続するコードの最初のアプローチを使用していますが、いくつかの問題のために/私はテーブルを手動で作成したので、マイグレーションコマンドは私のテーブルを作成していません。私は、objDbContext私のテーブルを取得することを意味します。テーブルの名前はデータベース内のタスクです。以下 はデフォルトEFで複数のテーブル名を生成します(私のコードの下にオブジェクト名 'dbo.Tasks'が無効です。コードの最初のアプローチ

eDbContext objDbContext = new eDbContext(); 
public List<TaskDetail> GetTasks(long eventId) 
     { 
      List<TaskDetail> listTask = new List<TaskDetail>(); 
      try { 

       listTask = (from task in objDbContext.Tasks 
          where task.EventId==eventId 
          select new TaskDetail 
          { 
           Id = task.Id, 
           Title = task.Title, 
           Description = task.Description, 
           StartDate = task.StartDate, 
           EndDate = task.EndDate 
          } 
          ).ToList(); 
      } 

      catch(Exception ex) { 
       throw ex; 
      } 
      return listTask; 

     } 

は、あなたが他のエンティティのために同様の問題(複数のテーブル名を)持っている場合は、あなたがPluralizingTableNameConventionを削除する必要があり、データベースのコンテキスト

public class eDbContext : DbContext 
    { 
     public DbSet<Task> Tasks { get; set; } 
    } 
+1

サイドノート: 'throw ex'はスタックトレースを書き換えます。 'スロー 'はうまくいく。サイド・サイド・ノート:なぜ「キャッチ」して「スロー」するの? – TheLethalCoder

+0

基本的にあなたはすべてが間違っていた... – Valkyrie

+0

だからバルキリー、私はそこに間違っていた。私が最初にコードすることに新しいので、私に示唆してください。 –

答えて

1

ですエンティティタイプ名から)。あなたのDbContextクラスにこのコードを追加します。

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
    base.OnModelCreating(modelBuilder); 
} 

他のテーブルには、複数の名前を持っている場合@Valkyrieeが示唆されているように、そしてあなただけTaskエンティティのマッピングを修正する必要があります。

0

あなたのDbContextクラスは次のようになります。あなたの移行のための

public class eDbContext : DbContext 
{ 
    public IebContext() 
     : base("name=ConnectionStringName") 
    { 

     Database.SetInitializer(new MigrateDatabaseToLatestVersion<eDbContext, Migrations.Configuration>("CatalogName")); 

    } 
    public DbSet<Task> Tasks{ get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Configurations.Add(new TaskMap()); 
    } 

} 

をあなたのような新しいクラスを作成することができます。

今、あなたはEFコード - を使用してテーブルを作成することができ、このアプローチを使用して
internal sealed class Configuration : DbMigrationsConfiguration<eDbContext> 
{ 
    public Configuration() 
    { 
     AutomaticMigrationsEnabled = true; 
     //know this might loss data while its true. 
     AutomaticMigrationDataLossAllowed = true; 
     ContextKey = "Path to your DbContext Class"; 
    } 

    protected override void Seed(eDbContext context) 
    { 
     // This method will be called after migrating to the latest version. 

     // You can use the DbSet<T>.AddOrUpdate() helper extension method 
     // to avoid creating duplicate seed data. E.g. 
     // 
     // context.People.AddOrUpdate(
     //  p => p.FullName, 
     //  new Person { FullName = "Andrew Peters" }, 
     //  new Person { FullName = "Brice Lambson" }, 
     //  new Person { FullName = "Rowan Miller" } 
     // ); 
     // 
    } 
} 

を後でそれらを変更してください。タスクのマップクラスを追加しました。つまり、エンティティマッピングに流暢なAPIを使用しています。

public class TaskMap : EntityTypeConfiguration<Task> 
{ 
    public TaskMap() 
    { 
     ToTable("Tasks"); 
     HasKey(x => x.Id); 
    } 
} 
関連する問題