[英]One to many relationship between AspNetUsers (Identity) and a custom table
I'm desperate trying to create an One to Many relationship between AspNetUsers table of Identity 2.0 and a custom table called Map (One user can have many maps, but a map can only have one user) I've tryied mostly every solutions available in this website and also lost many days trying other soulutions found in the web. 我迫切希望在Identity 2.0的AspNetUsers表和一个名为Map的自定义表之间创建一对多的关系(一个用户可以有很多地图,但一个地图只能有一个用户)我已经尝试了大多数可用的解决方案这个网站也失去了很多天尝试在网络上找到的其他soulutions。 I'm stuck. 我被卡住了。 Nothing seems to work for me. 似乎没有什么对我有用。 I'm new to MVC and EF so basically I think I need some sort of step-by-step guide. 我是MVC和EF的新手,所以基本上我认为我需要一些循序渐进的指南。 Can anyone please be very kind to provide me a newbie guide to achieve this from a fresh made MVC5 Project. 任何人都可以非常友好地为我提供一个新手指南,以实现这一点,从一个新的MVC5项目。 Thanks and sorry for the redundad question I've made. 谢谢,对不起我提出的冗余问题。
PS. PS。 I can and I'm successful on creating everykind of relationshipt between two custom tables, but not between AspNetUsers and my Map table. 我可以成功创建两个自定义表之间的关系,但不能在AspNetUsers和我的Map表之间创建。 Thanks very very much in advance. 非常感谢提前。
I have done exactly this on a number of projects 我在很多项目中做到了这一点
For instance i have a one to many relationship from ASPNetUsers to Notifications. 例如,我有一个从ASPNetUsers到Notifications的一对多关系。 So in my ApplicationUser class inside IdentityModels.cs i have 所以我在IdentityModels.cs里面的ApplicationUser类中有
public virtual ICollection<Notification> Notifications { get; set; }
My Notifications class has the reverse 我的通知类具有相反的功能
public virtual ApplicationUser ApplicationUser { get; set; }
By default EF will then create a cascade delete from Notification to AspNetUsers which i dont want - so i also have this in my Context class 默认情况下,EF将创建从Notification到AspNetUsers的级联删除,我不想要 - 所以我也在我的Context类中有这个
modelBuilder.Entity<Notification>()
.HasRequired(n => n.ApplicationUser)
.WithMany(a => a.Notifications)
.HasForeignKey(n => n.ApplicationUserId)
.WillCascadeOnDelete(false);
Just remember the definition for AspNetUSers is extended in the ApplicationUser class inside IdentityModels.cs that is generated for you by visual studios scaffolding. 请记住,AspNetUSers的定义是在Visual Studio脚手架为您生成的IdentityModels.cs中的ApplicationUser类中进行了扩展。 Then treat it as any other class/table in your app 然后将其视为应用中的任何其他类/表
UPDATE - here are examples of full models 更新 - 以下是完整模型的示例
public class ApplicationUser : IdentityUser
{
[StringLength(250, ErrorMessage = "About is limited to 250 characters in length.")]
public string About { get; set; }
[StringLength(250, ErrorMessage = "Name is limited to 250 characters in length.", MinimumLength=3)]
public string Name { get; set; }
public DateTime DateRegistered { get; set; }
public string ImageUrl { get; set; }
public virtual ICollection<Notification> Notifications { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class Notification
{
public int ID { get; set; }
public int? CommentId { get; set; }
public string ApplicationUserId { get; set; }
public DateTime DateTime { get; set; }
public bool Viewed { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public virtual Comment Comment { get; set; }
}
} }
http://www.itorian.com/2013/11/customizing-users-profile-to-add-new.html http://www.itorian.com/2013/11/customizing-users-profile-to-add-new.html
this post contains your answer 这篇文章包含你的答案
Here is a step by step solution with visuals (It uses inheritance): 这是一个带有视觉效果的逐步解决方案(它使用继承):
https://practiceaspnet.wordpress.com/2015/04/03/adding-asp-net-identity-to-an-existing-entity-framework-asp-net-web-forms-application/ https://practiceaspnet.wordpress.com/2015/04/03/adding-asp-net-identity-to-an-existing-entity-framework-asp-net-web-forms-application/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.