将 MVC 4 SimpleMembership 与现有的数据库优先 EF 模型一起使用 [英] Using MVC 4 SimpleMembership with an existing database-first EF model

查看:37
本文介绍了将 MVC 4 SimpleMembership 与现有的数据库优先 EF 模型一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试在我的 MVC 4 中使用 SimpleMembership,我已经有一个现有的数据库和基于它创建的 EF5 模型!我搜索了很多,但我找不到如何在我的情况下使用它以及如何在我自己的模型下使用它.

I am trying to use SimpleMembership in my MVC 4 for the first time and I already have an existing database and EF5 model created based on it! I searched a lot but I cant find how I could use it in my case and also to have everything under my own model.

如果有人能告诉我如何做到这一点就好了.

It would be great if somebody can give me an idea how to do this.

谢谢

推荐答案

纯粹作为参考,创建一个新的 ASP.NET MVC 4 Web 应用程序项目的 Internet 应用程序模板可能是个好主意(即通过文件 > 新项目).

Purely as a point of reference, it might be a good idea to create a new Internet Application template of an ASP.NET MVC 4 Web Application project (i.e. via File > New Project).

如果您查看 AccountController,正如@zms6445 所说,它是用 InitializeSimpleMembership 属性修饰的.您可以在根目录下 Filters 文件夹中的 InitializeSimpleMembershipAttribute.cs 文件中找到该属性的实现.

If you look at the AccountController, as @zms6445 says, it is decorated with an InitializeSimpleMembership attribute. You can find the implementation of this attribute in the InitializeSimpleMembershipAttribute.cs file in the Filters folder within the root directory.

在这里,这是拼图的缺失部分 - 您需要连接现有的数据库,以便 SimpleMembershipProvider 使用它.这是您需要的代码:

In here, this is the missing part of the puzzle - you need to hook up your existing database so that it is used by the SimpleMembershipProvider. This is the code you need:

private class SimpleMembershipInitializer
{
    public SimpleMembershipInitializer()
    {
        try
        {
            if (!WebSecurity.Initialized)
            {
                WebSecurity.InitializeDatabaseConnection("CONNECTION_STRING_NAME", "USER_TABLE", "USER_ID_FIELD", "USER_NAME_FIELD", autoCreateTables: true);
            }
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException("Something is wrong", ex);
        }
    }
}

注意事项:

  1. CONNECTION_STRING_NAME 是 web.config ConnectionStrings 中的一个条目 - 您不能在此处使用模型连接字符串 - SimpleMembershipProvider 无法识别该格式!您需要指定一个 System.Data.SqlClient 连接字符串,例如

  1. CONNECTION_STRING_NAME is an entry in your web.config ConnectionStrings - you CANNOT use the model connection string here - the SimpleMembershipProvider does not recognise that format! You need to specify an System.Data.SqlClient connection string, e.g.

<add name="CONNECTION_STRING_NAME" connectionString="data source=SERVER;initial catalog=DATABASE;user id=USER;password=PASSWORD;"providerName="System.Data.SqlClient"/>

<add name="CONNECTION_STRING_NAME" connectionString="data source=SERVER;initial catalog=DATABASE;user id=USER;password=PASSWORD;" providerName="System.Data.SqlClient" />

USER_TABLE 是数据库中用于保存额外用户信息(例如名字、姓氏等)的表.它通过 USER_ID_FIELD 链接到自动生成的表.

USER_TABLE is the table in your database to hold extra user information, such as first name, surname etc. This is linked to the autogenerated tables via the USER_ID_FIELD.

USER_ID_FIELD 通常是用户表的主键.它必须是 int 类型.

USER_ID_FIELD is usually the primary key of your Users table. It must be of type int.

USER_ID_NAME 是用户的唯一名称,可以是电子邮件地址.

USER_ID_NAME is a unique name for the user, which could be an Email address.

autoCreateTables 设置为 true 以确保 SimpleMembership 工作所需的表在不存在的情况下被创建.

autoCreateTables is set to true to ensure the tables required for the SimpleMembership to work are created if they don't already exist.

当然,只有当您通过 AccountController 点击页面时,才会触发此代码,因为它已由属性修饰.您可以在其中放置一个断点并查看其运行情况.

Of course, this code only gets fired if you hit a page via the AccountController, since this has been decorated by the attribute. You could put a breakpoint in there and see it in action.

这应该能让您入门 - 如果您遇到困难,Internet 应用程序模板是一个非常好的模板.

This should get you started - the Internet Application template is a pretty good template to follow if you get stuck.

希望这会有所帮助.

这篇关于将 MVC 4 SimpleMembership 与现有的数据库优先 EF 模型一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆