[英]Replacing DbContext in Microsoft MVC Individual User Accounts Template
我刚刚创建了我的第一个MVC网站。 为了避免自己编写控制器程序,我使用了Microsoft的“ 个人用户帐户”模板。
我知道,该模板使用实体框架创建了一个快速数据库来保留用户/帐户数据。
由于我已经有要使用的数据库,因此我想更改模板,以便它使用DbContext表示数据库。
我能够更改connectionString,以便在数据库中创建模板的表。 但是我不希望它创建自己的表,而是使用已经创建的表。
有没有简单的方法可以做到这一点?
还是我应该自己重新编写整个帐户/用户控制器?
// This is an example of DbContext class it implements DbContext
// If you do not use constructor(s) then the expectation by entity framework
// will be that your name of your connectionstring in web.config
// or app.config is name name as your class so e.g. "YourContext",
// otherwise "Name="YourConnectionString"
public class YourContext : DbContext
{
// constructor as you wish /want
public YourContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{ }
// critical mapping
public DbSet<someModel> someModel { get; set; }
// critical overide
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// critical key to NOT let your database get dropped or created etc...
Database.SetInitializer<YourContext>(null);
// This is an example of mapping model to table
// and also showing use of a schema ( dbo or another )
modelBuilder.Entity<someModel>().ToTable("someTable", schemaName: "dbo");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.