I am running with SQL server express 2008 R2 with a database and SQL authentication. I have added the database connnection to my server explorer and added the connection string to web.config
file. When I debug the application it seems to be creating a default Database e.g. tutorial.MVC
etc., instead of picking up the database I created prior to creating the app.
我使用SQL server express 2008 R2运行,使用数据库和SQL身份验证。我已经将数据库连接添加到服务器浏览器并将连接字符串添加到web。配置文件。当我调试应用程序时,它似乎正在创建一个默认的数据库,例如教程。MVC等,而不是在创建应用程序之前获取我创建的数据库。
Also I am getting the compilation Error: {"Invalid object name 'dbo.Products'."}
which should occur because the SQL is:
我也得到了编译错误:{“无效的对象名称‘dbo.Products’。”因为SQL是:
{SELECT
[Extent1].[ProductID] AS [ProductID],
[Extent1].[ProductName] AS [ProductName],
[Extent1].[ProductPrice] AS [ProductPrice]
FROM [dbo].[Products] AS [Extent1]}
There exists no table called products. This must have been generated by the entity framework database. Which I have deleted because the Products model class was originially Products where as the database table was called Product (human error) so it is failing to update the SQL. Essentially I just want the controller to map to the correct database. I suspect it isn't picking up the database from the connectionString. Although the connection string is being picked up by StoreContext when debugging. Seems not to be picking up the database. But can see why Web.config
没有称为product的表。这一定是由实体框架数据库生成的。我已经删除了,因为Product model类是原始产品,因为数据库表被称为Product (human error),所以它没有更新SQL。本质上,我只想让控制器映射到正确的数据库。我怀疑它不是从connectionString获取数据库。尽管在调试时,StoreContext会选择连接字符串。好像不是在拿数据库。但是可以看到为什么是Web.config
<configuration>
<connectionStrings>
<add
name="StoreContext"
connectionString="Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=test;Integrated Security=True;User ID=Login;Password=***********"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
global.asax
global.asax
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Database.SetInitializer<Tutorial.Models.StoreContext>(null);
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
Product Controller
产品控制器
public class ProductController : Controller
{
StoreContext hello = new StoreContext();
//
// GET: /Product/
public ActionResult Index()
{
var product = hello.Product.ToList();
return View(product);
}
}
Product Model:
产品型号:
{
[Table("Product")]
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public double ProductPrice { get; set; }
}
}
StoreContext:
StoreContext:
public class StoreContext: DbContext
{
public DbSet<Product> Product { get; set; }
}
1 个解决方案
#1
2
Have you set the correct DatabaseInitializer in your project?
您是否在项目中设置了正确的DatabaseInitializer ?
IDatabaseInitializer contains 4 derived types that you can use: CreateDatabaseIfNotExists, DropCreateDatabaseAlways, DropCreateDatabaseIfModelChanges and MigrateDatabaseToLatestVersion
IDatabaseInitializer包含4种可使用的派生类型:createdatabaseifnotexist、DropCreateDatabaseAlways、DropCreateDatabaseIfModelChanges和MigrateDatabaseToLatestVersion
Choose the initializer that fit your needs and derive from it.
选择符合您的需要并从中派生的初始化器。
Next, on constructor of your context, set the initializer:
接下来,在上下文的构造函数上,设置初始化器:
public Context() : base("ConnectionString")
{
Database.SetInitializer(new MigrationsDatabaseInitializer());
}
MigrationsDatabaseInitializer is my derived class from MigrateDatabaseToLatestVersion.
MigrationsDatabaseInitializer是来自MigrateDatabaseToLatestVersion的派生类。
Maybe your project is always recriating the database over and over.
也许您的项目总是不断地重新编写数据库。
You can disable pluralization with convention. Override OnModelCreation and remove pluralization convention:
可以使用约定禁用多元化。重写OnModelCreation并删除多元化约定:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
Hope it helps! :)
希望它可以帮助!:)
#1
2
Have you set the correct DatabaseInitializer in your project?
您是否在项目中设置了正确的DatabaseInitializer ?
IDatabaseInitializer contains 4 derived types that you can use: CreateDatabaseIfNotExists, DropCreateDatabaseAlways, DropCreateDatabaseIfModelChanges and MigrateDatabaseToLatestVersion
IDatabaseInitializer包含4种可使用的派生类型:createdatabaseifnotexist、DropCreateDatabaseAlways、DropCreateDatabaseIfModelChanges和MigrateDatabaseToLatestVersion
Choose the initializer that fit your needs and derive from it.
选择符合您的需要并从中派生的初始化器。
Next, on constructor of your context, set the initializer:
接下来,在上下文的构造函数上,设置初始化器:
public Context() : base("ConnectionString")
{
Database.SetInitializer(new MigrationsDatabaseInitializer());
}
MigrationsDatabaseInitializer is my derived class from MigrateDatabaseToLatestVersion.
MigrationsDatabaseInitializer是来自MigrateDatabaseToLatestVersion的派生类。
Maybe your project is always recriating the database over and over.
也许您的项目总是不断地重新编写数据库。
You can disable pluralization with convention. Override OnModelCreation and remove pluralization convention:
可以使用约定禁用多元化。重写OnModelCreation并删除多元化约定:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
Hope it helps! :)
希望它可以帮助!:)