如何将成员资格表与实体框架集成? Asp.net

时间:2022-09-07 15:13:44

I have never used Entity Framework in a project before so I am not sure where to begin. I have I am using the membership tables that are created when using ASP.Net membership provider as is.

我之前从未在项目中使用Entity Framework,所以我不知道从哪里开始。我正在使用在使用ASP.Net成员资格提供程序时创建的成员资格表。

I would like to create an object that contains a reference to a user. For example

我想创建一个包含对用户的引用的对象。例如

public int Id {get;set;}
public User User{get;set} // Where User is some object relating to Membership provider User.
public string Application {get; set;}

I am using a code first approach to using EF so I would like to let it generate tables based on this object. So how do I configure this so that EF will recognize that I am creating a relationship to a user managed by asp.net Membership? What type should I use in the above example in place of 'User' (what is the asp.net type)

我使用代码第一种方法来使用EF,所以我想让它基于这个对象生成表。那么我该如何配置这个以便EF能够识别我正在创建一个与asp.net Membership管理的用户的关系?我应该在上面的示例中使用什么类型代替'User'(什么是asp.net类型)

Sorry if this is vague. I basically just need an example or explanation of how EF integrates with Asp.net membership provider schema.

对不起,如果这是模糊的。我基本上只需要一个示例或解释EF如何与Asp.net成员资格提供者架构集成。

Thanks!

2 个解决方案

#1


6  

If you want to have one EF model which links your membership tables and application tables, you can add the ASP.NET Membership tables to your existing database using the aspnet_regsql.exe utility. Some links:

如果要使用一个链接成员资格表和应用程序表的EF模型,可以使用aspnet_regsql.exe实用程序将ASP.NET成员资格表添加到现有数据库中。一些链接:

Once you have the Membership tables installed, you can generate EF Code classes that match the membership schema. I posted a detailed walkthrough on how to do that here:

安装成员资格表后,可以生成与成员资格模式匹配的EF代码类。我在这里发布了有关如何执行此操作的详细演练:

Generating EF Code First model classes from an existing database

从现有数据库生成EF Code First模型类

#2


2  

I would say you could create an "AppUser"(Your own custom User object) Entity where the corresponding table has a foreign key to the "UserID" column of the aspnet_Membership table. This way it will be easier if you need to change or add properties to your "AppUser" Entity instead of trying to change the MS table structure (which can be a real pain). You can still interact with the built-in MS Membership classes and functions from your MVC project using something like the MvcMembership starter Kit DLL's.

我想你可以创建一个“AppUser”(你自己的自定义用户对象)实体,其中相应的表具有aspnet_Membership表的“UserID”列的外键。这样,如果您需要更改或添加“AppUser”实体的属性而不是尝试更改MS表结构(这可能是一个真正的痛苦),将会更容易。您仍然可以使用类似MvcMembership入门套件DLL的内容与MVC项目中的内置MS Membership类和函数进行交互。

https://github.com/TroyGoode/MembershipStarterKit

Hope this helps!

希望这可以帮助!

#1


6  

If you want to have one EF model which links your membership tables and application tables, you can add the ASP.NET Membership tables to your existing database using the aspnet_regsql.exe utility. Some links:

如果要使用一个链接成员资格表和应用程序表的EF模型,可以使用aspnet_regsql.exe实用程序将ASP.NET成员资格表添加到现有数据库中。一些链接:

Once you have the Membership tables installed, you can generate EF Code classes that match the membership schema. I posted a detailed walkthrough on how to do that here:

安装成员资格表后,可以生成与成员资格模式匹配的EF代码类。我在这里发布了有关如何执行此操作的详细演练:

Generating EF Code First model classes from an existing database

从现有数据库生成EF Code First模型类

#2


2  

I would say you could create an "AppUser"(Your own custom User object) Entity where the corresponding table has a foreign key to the "UserID" column of the aspnet_Membership table. This way it will be easier if you need to change or add properties to your "AppUser" Entity instead of trying to change the MS table structure (which can be a real pain). You can still interact with the built-in MS Membership classes and functions from your MVC project using something like the MvcMembership starter Kit DLL's.

我想你可以创建一个“AppUser”(你自己的自定义用户对象)实体,其中相应的表具有aspnet_Membership表的“UserID”列的外键。这样,如果您需要更改或添加“AppUser”实体的属性而不是尝试更改MS表结构(这可能是一个真正的痛苦),将会更容易。您仍然可以使用类似MvcMembership入门套件DLL的内容与MVC项目中的内置MS Membership类和函数进行交互。

https://github.com/TroyGoode/MembershipStarterKit

Hope this helps!

希望这可以帮助!