2016-08-24 7 views
1

私はEntityframeworkを初めて使用しています。私は、entityFrameworkのCodefirstアプローチを使用してデータベースを作成しようとしています。私はここDALの層に私のコードを持っている:これは、SQL Serverのデータベースを作成しませんEntityFramework-CodeFirstアプローチでデータベースが作成されない

<connectionStrings> 
<add name="DbConnection" 
    connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=dbPdtCgry;uid=admin;password=admin123;" 
     providerName="System.Data.SqlClient"></add> 

DAL層のapp.configファイル内

[Table("Category")] 
public class Category 
{ 
    [Key] 
    public int CategoryId { get; set; } 

    [Required] 
    [Column(TypeName="varchar")] 
    [StringLength(200)] 
    public string CategoryName { get; set; } 
} 


public class DatabaseContext:DbContext 
{ 
    public DatabaseContext() 
     : base("DbConnection") 
    { 
     //Added the following but still it doesnt work 
     //Database.SetInitializer<DatabaseContext>(new CreateDatabaseIfNotExists<DatabaseContext>()); 
    } 

    public DbSet<Category> Categories { get; set; } 
} 

接続文字列management.But DbConnection Log file & mdf fileがApp_Dataに作成され、データの追加、更新、削除が可能です。

私はここに何かが欠けていますか?

答えて

1

あなたは、私が知っているDBあなたのApp_Dataにに作成されますが、SQLサーバー

+0

「Server = localhost \ SQLserverInstanceName」を設定していただきありがとうございます – ksg

1

のデフォルトのフォルダに、私は常にステップで、このステップのようにシードされなかったそれのために、接続文字列でサーバ= localhostの\ SQLserverInstanceNameを逃したことがあり私は何かが欠けているわけではない。

<connectionStrings> 
    <add name="LibraryDb" connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\LibraryDb.mdf;Integrated Security=True; MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 

私のクラス

public class Author 
{ 
    [Key] 
    public int id { get; set; }   
    [Required(ErrorMessage = "You Need to Enter A Author Name ")] 
    public string AuthorName { get; set; } 
    public string Address { get; set; } 
    public virtual List<book> books{ get; set; } 

} 
public class Book 
{ 
    public int id { get; set; } 
    public string BookName { get; set; } 
    [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")] 
    public DateTime DateofPublish { get; set; } 
    public int id { get; set; } 

    public virtual Author Author{ get; set; } 
} 

public class LibraryDb: DbContext 
{ 
    public DbSet<Author> Authors{ get; set; } 
    public DbSet<Book> Books{ get; set; } 

    public LibraryDb() : base("LibraryDb") { } 

} 

はその後、私のIntialiserクラスでは、私はそう

Database.SetInitializer(new LandLordInitiliser()); 

を追加するのglobal.asaxで

public class LibraryInitiliser:DropCreateDatabaseAlways<LibraryDb> 
{ 
    protected override void Seed(LibraryDb context) 
    { 
     var book= new List<Author> 
     { 
      new Author() { AuthorName = "Naame1", Address = "dublin", Book= new List<Book> 
      { 
       new Book() { BookName = "Naae1", PublishDate= DateTime.Parse("01/02/1981") }, 
       new Book() { BookName = "Naae1", PublishDate= DateTime.Parse("01/02/1981") }, 
       new Book() { BookName = "Naae1", PublishDate= DateTime.Parse("01/02/1981") } 
      } }, 
      new Author() { AuthorName = "Naame1", Address = "dublin", Book= new List<Book> 
      { 
       new Book() { BookName = "Naae1", PublishDate= DateTime.Parse("01/02/1981") }, 
       new Book() { BookName = "Naae1", PublishDate= DateTime.Parse("01/02/1981") }, 
       new Book() { BookName = "Naae1", PublishDate= DateTime.Parse("01/02/1981") } 
      } }, 

      } } 

     }; 

     book.ForEach(m => context.Library.Add(m)); 
     context.SaveChanges(); 

     base.Seed(context); 
    } 
} 

と思い出してくれるの種子にこれを行いますデータはシードされません。

希望が助けてくれる

関連する問題