问题描述
我很想在Identity 2.0的AspNetUsers表和一个名为Map的自定义表之间创建一对多关系(一个用户可以有很多地图,但是一个地图只能有一个用户)该解决方案可从该网站上获得,也浪费了很多时间尝试网络上发现的其他灵魂.我被卡住了.似乎对我没有任何帮助.我是MVC和EF的新手,所以基本上我认为我需要某种逐步指南.任何人都可以请我提供一个新手指南,以帮助您从全新制作的MVC5项目中实现这一目标.感谢和抱歉,我已经提出了关于冗余的问题.
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. 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. Can anyone please be very kind to provide me a newbie guide to achieve this from a fresh made MVC5 Project. Thanks and sorry for the redundad question I've made.
PS.我可以而且我已经成功创建了两个自定义表之间的各种关系,但是没有在AspNetUsers和我的Map表之间创建关系.提前非常感谢.
PS. I can and I'm successful on creating everykind of relationshipt between two custom tables, but not between AspNetUsers and my Map table. Thanks very very much in advance.
推荐答案
我已经在许多项目上做到了这一点
I have done exactly this on a number of projects
例如,我有从ASPNetUsers到Notifications的一对多关系.因此,在我的IdentityUsers.cs中的ApplicationUser类中,我有
For instance i have a one to many relationship from ASPNetUsers to Notifications. So in my ApplicationUser class inside IdentityModels.cs i have
public virtual ICollection<Notification> Notifications { get; set; }
我的通知类具有相反的作用
My Notifications class has the reverse
public virtual ApplicationUser ApplicationUser { get; set; }
默认情况下,EF然后会从Notification到我不希望的AspNetUsers创建级联删除-因此我的Context类中也有此删除
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
modelBuilder.Entity<Notification>()
.HasRequired(n => n.ApplicationUser)
.WithMany(a => a.Notifications)
.HasForeignKey(n => n.ApplicationUserId)
.WillCascadeOnDelete(false);
请记住,AspNetUSers的定义是在IdentityModels.cs中的ApplicationUser类中扩展的,该类是由Visual Studio脚手架为您生成的.然后将其视为您应用中的任何其他类/表
Just remember the definition for AspNetUSers is extended in the ApplicationUser class inside IdentityModels.cs that is generated for you by visual studios scaffolding. 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; }
}
}
这篇关于AspNetUsers(身份)和自定义表之间的一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!