CodeFirst生成数据库
我最近尝试使用 CodeFirst,但我想知道一些事情。 我使用CF生成db(MyDB.mdf),然后删除SQL Server中的一个表(Category)。接下来,当我尝试重新创建新表时,CF 没有在我的数据库中生成新表。 这是我的代码
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; }
public DbSet<Category> Categories { get; set; }
}
这是我的连接字符串,以便 CF 可以生成到我的 SQL Server 的数据库
<add name="MyDB" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
我不使用 SqlCe 数据库 b/ci 无法打开它
任何解决方案。
谢谢
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; }
public 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要添加
到 global.asax 文件中的应用程序启动方法
EF4 Code First 创建新表
you need to add
to the application start method in your global.asax file
EF4 Code First create new table