未在EntityFramework CodeFirst方法上创建数据库

本文关键字:创建 数据库 方法 EntityFramework CodeFirst 未在 | 更新日期: 2023-09-27 18:03:39

我是Entityframework的新手。我正在尝试使用Entityframework的Codefirst方法创建数据库。我在DAL层中有我的代码:

 [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服务器管理中创建数据库。但DbConnection Log file & mdf file是在App_Data中创建的,我可以添加、更新和删除数据。

我是不是遗漏了什么??

未在EntityFramework CodeFirst方法上创建数据库

您可能在连接字符串中错过了Server=localhost''SQLserverInstanceName,因为DB是在您的App_data上创建的,而不是在SQL Server 的默认文件夹中

我总是这样一步一步地播种,然后我知道我没有错过任何东西。

   <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") { }
}

然后在我的亲密课堂上,我这样做是为了播种

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

否则将不播种任何数据。

希望能帮助