2016-08-24 34 views
1

我是Entityframework.新嘗試使用CodeFirst方法entityFramework創建數據庫。我有我的代碼在DAL層的位置:在EntityFramework-CodeFirst方法上未創建數據庫

[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; } 
} 

連接字符串中dal層App.config文件

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

在SQL Server這不會創建數據庫management.But在App_Data中創建了DbConnection Log file & mdf file,我可以添加,更新和刪除數據。

我在這裏想念什麼?

回答

1

可能是你錯過了在連接字符串服務器=本地主機\ SQLserverInstanceName,因爲它DB是您APP_DATA創建的,但不是在你的SQL服務器的默認文件夾

+0

感謝設置'Server = localhost \ SQLserverInstanceName'解決了問題 – ksg

1

我總是種子這樣一步一步我就知道我不會錯過任何東西。

<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類我這樣做種子

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); 
    } 
} 

而且Remeber在Global.asax添加

Database.SetInitializer(new LandLordInitiliser()); 

否則沒有數據將被播種。

希望幫助