在EF6中设置一对多关系 [英] Setup one to many relationship in EF6
本文介绍了在EF6中设置一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下两个课程
// < span class =code-comment>您可以通过向ApplicationUser类添加更多属性来为用户添加配置文件数据,请访问http://go.microsoft.com/fwlink/?LinkID=317594以了解更多信息。
public class AppUser:IdentityUser
{
public async 任务< ClaimsIdentity> GenerateUserIdentityAsync(UserManager< AppUser> manager)
{
// 注意authenticationType必须与在CookieAuthenticationOptions.AuthenticationType 中定义
var userIdentity = await manager.CreateIdentityAsync(< span class =code-keyword> this ,DefaultAuthenticationTypes.ApplicationCookie);
// 在此处添加自定义用户声明
返回 userIdentity;
}
public int ? DomainId { get ; set ; }
[ForeignKey( DomainId)]
< span class =code-keyword> public virtual 域Domain { get ; set ; }
}
public class 域
{
[Key]
public int DomainId {获得跨度>; set ; }
[必填]
[MaxLength( 100 )]
public string 名称{ get ; set ; }
public virtual ICollection< AppUser> AppUsers { get ; set ; }
}
我试图通过数据注释设置一对多的关系,我无法让它工作正确。
创建字段DomainId为可空,并使用Domain表的外键,但它也创建了一个名为Domain_DomainId的字段作为可以为空并且使用域表的外键。
如何让Domain_DomainId字段消失?
解决方案
您将错误的项目标记为外键。
public class AppUser:IdentityUser
{
public < span class =code-keyword> async 任务< claimsidentity> GenerateUserIdentityAsync(UserManager< appuser> manager)
{
// 注意authenticationType必须与在CookieAuthenticationOptions.AuthenticationType 中定义
var userIdentity = await manager.CreateIdentityAsync(< span class =code-keyword> this ,DefaultAuthenticationTypes.ApplicationCookie);
// 在此处添加自定义用户声明
返回 userIdentity;
}
[ForeignKey( Domain)]
public int ? DomainId { get ; set ; }
public virtual 域Domain {获得跨度>; set ; }
}
< / appuser > < / claimsidentity > 跨度>
I have the following two classes
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class AppUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<AppUser> 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 int? DomainId { get; set; }
[ForeignKey("DomainId")]
public virtual Domain Domain { get; set; }
}
public class Domain
{
[Key]
public int DomainId { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
public virtual ICollection<AppUser> AppUsers { get; set; }
}
and I am trying to get a one to many relationship setup via dataannotations and I cannot get it to work correctly.
It is creating the field "DomainId" as nullable and with a foreign key to the Domain table, but it is also creating a field called "Domain_DomainId" as nullable and with a foreign key to the Domain table.
How do I get the "Domain_DomainId" field to go away?
解决方案
You have the wrong item tagged as the foreign key.
public class AppUser : IdentityUser { public async Task<claimsidentity> GenerateUserIdentityAsync(UserManager<appuser> 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; } [ForeignKey("Domain")] public int? DomainId { get; set; } public virtual Domain Domain { get; set; } } </appuser></claimsidentity>
这篇关于在EF6中设置一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文