查看示例它看起来非常简单,但我无法使用代码优先方法创建数据库。
我的模特课:
public class Books
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
DbContext
:
public class BookDBContext : DbContext
{
public DbSet<Books> Books { get; set; }
public BookDBContext() : base("name=BookDBContext")
{
}
}
控制器代码:
public ViewResult CodeFirst()
{
BookDBContext context = new BookDBContext();
List<Books> result = new List<Books>();
result = context.Books.ToList();
return View();
}
配置:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-EF-20150919111248;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-EF-20150919111248.mdf"
providerName="System.Data.SqlClient" />
<add name="NorthwindEntities"
connectionString="metadata=res://*/Controllers.EF.csdl|res://*/Controllers.EF.ssdl|res://*/Controllers.EF.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Northwind;user id=imad;password=imad123;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="DBEntities"
connectionString="metadata=res://*/Controllers.DB.csdl|res://*/Controllers.DB.ssdl|res://*/Controllers.DB.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Northwind;user id=imad;password=imad123;multipleactiveresultsets=True;application name=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="DBEEntities"
connectionString="metadata=res://*/Models.DB.csdl|res://*/Models.DB.ssdl|res://*/Models.DB.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Northwind;user id=imad;password=imad123;multipleactiveresultsets=True;application name=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="BookDBContext"
connectionString="Data Source=.;Initial Catalog=Sample;User Id=imad;Password=imad123;"
providerName="System.Data.EntityClient"/>
</connectionStrings>
最后一个是我的。
调试器进出控制器。没有在ssms中创建的DB没有生成edmx。我还需要做什么?
更新
我没有使用EF
安装Nuget
。刚从其他应用中引用了EntityFramework.dll
。但它正在Schema First
方法中工作。
感谢。