简体   繁体   English

在Microsoft MVC个人用户帐户模板中替换DbContext

[英]Replacing DbContext in Microsoft MVC Individual User Accounts Template

I just created my first MVC Website. 我刚刚创建了我的第一个MVC网站。 To avoid having to program the controller myself, I am using the Individual User Accounts template from Microsoft. 为了避免自己编写控制器程序,我使用了Microsoft的“ 个人用户帐户”模板。

I know, that this template uses the Entity Framework to create an express database to persist the user/account data. 我知道,该模板使用实体框架创建了一个快速数据库来保留用户/帐户数据。

Since I already have a database, which I want to use, I want to change the template so it uses the DbContext for said database. 由于我已经有要使用的数据库,因此我想更改模板,以便它使用DbContext表示数据库。

I was able to change the connectionString, so that the tables of the template got created in my database. 我能够更改connectionString,以便在数据库中创建模板的表。 But I don't want it to create it's own tables but use my already created tables. 但是我不希望它创建自己的表,而是使用已经创建的表。

Is there any easy way to achieve this? 有没有简单的方法可以做到这一点?

Or should I just write the whole account/user controller from scratch myself? 还是我应该自己重新编写整个帐户/用户控制器?

// 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.

相关问题 一对多MVC个人用户帐户 - One-to-many MVC Individual User Accounts MVC 个人用户帐户登录值持久化 - MVC Individual User Accounts login value persisting 使用个人用户帐户将 IdentityServer 添加到 Razor 应用程序 - 找到多个 DbContext - Adding IdentityServer to Razor app with Individual User Accounts - More than one DbContext was found ASP .NET MVC数据库第一个个人用户帐户 - ASP .NET MVC Database First Individual User Accounts 个人用户帐户在ASP.Net MVC中不进行身份验证 - Individual User Accounts to No Authentication in ASP.Net MVC 在MVC 5应用程序上找不到SignalR / signalr / hubs 403(单个用户帐户身份验证) - SignalR /signalr/hubs 403 Not found on MVC 5 Application (Individual user accounts authentication) 如何在Azure中发布支持身份(单个用户帐户)的我的MVC应用程序? - How published in Azure my MVC App with support for Identity (Individual User Accounts)? 如何在MVC与2017中设置个人用户帐户和启用迁移 - how set individual user accounts and enable-migrations in mvc vs 2017 .Net Core 3.1 MVC 身份验证个人用户帐户和 ASP.NET 核心项目中的脚手架身份 - .Net Core 3.1 MVC Authentication Individual User Accounts and Scaffold Identity in ASP.NET Core projects ASP.NET MVC 5 Web应用程序中是否应该只有一个DbContext和数据库才能将用户帐户与对象相关联? - Should there be single DbContext and Database within the ASP.NET MVC 5 Web Application to be able associate User's Accounts with objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM