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

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

问题描述

我想使用SimpleMembership在我的MVC 4,第一次,我已经有了基础上创建的现有数据库和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应用程序项目的一个新的互联网应用程序模板是一个好主意(比如通过文件>新建项目)。

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 属性。你可以找到这个属性在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 。这是code你需要:

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);
        }
    }
}

需要注意以下几点:

Some things to note:


  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.

<添加名称=CONNECTION_STRING_NAME的connectionString =数据源=服务器;初始目录=数据库;用户ID =用户;密码=密码;的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 是对于用户来说,这可能是一个Email地址的唯一名称。

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

autoCreateTables 设置为真正来确保为SimpleMembership到如果创建工作所需的表它们不存在。

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

当然,这code只得到,如果你通过打一个页面打响了的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.

这应该让你开始 - 互联网应用程序模板是pretty好的模板,如果您遇到遵循

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天全站免登陆