开发者

CodeFirst generating database

开发者 https://www.devze.com 2023-03-28 06:38 出处:网络
I\'ve tried using CodeFirst lately and I\'m wondering something. I used CF to generate db(MyDB.mdf), then I deleted a table (Category) in SQL Server. Next, when I\'ve tried to recreate a new table, CF

I've tried using CodeFirst lately and I'm wondering something. I used CF to generate db(MyDB.mdf), then I deleted a table (Category) in SQL Server. Next, when I've tried to recreate a new table, CF didn't generate new table in my database. Here's my code

public class Product
{        
    public int prodID { get; set; }        
    public int catID { get; set; }
    public string prodName { get; set; }        
    public Double Price { get; set; }

    public virtual Category Categories { get; set; }
}

public class Category
{        
    public int catID { get; set; }
    public string catName { get; set; }

    public virtual ICollection<Product> Products { get; set; }
}

public class MyDB : DbContext
{
    public DbSet<Product> Products { get; set; }
    p开发者_StackOverflow中文版ublic DbSet<Category> Categories { get; set; }
}

And here's my connection string so CF can generate db to my SQL Server

<add name="MyDB" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True" providerName="System.Data.SqlClient"/>

I don't use SqlCe database b/c i can't open it

Any solutions.

Thanks


you need to add

 Database.SetInitializer(new DropCreateDatabaseIfModelChanges<YourContext>());

to the application start method in your global.asax file EF4 Code First create new table

0

精彩评论

暂无评论...
验证码 换一张
取 消