编码第一个 EF 6 自动生成关系 table 与附加列
Code first EF 6 auto generated relation table with additional column
我正在尝试首先在代码中创建具有多对多关系的新 table。
我有两个 table:用户和项目。现在,我想为这两个 table 建立多对多关系。所以我做以下事情:
public class Project
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string PositionCount { get; set; }
public bool IsActive { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public class User : IdentityUser<Guid, UserLogin, UserRole, UserClaim>
{
public override Guid Id { get; set; }
public string Tags { get; set; }
public User()
{
Id = Guid.NewGuid();
}
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, Guid> 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 virtual ICollection<Project> Projects { get; set; }
}
public class RecruitmentDbContext : IdentityDbContext<User, MyRole, Guid, UserLogin, UserRole, UserClaim>
{
public DbSet<Project> Projects { get; set; }
public DbSet<UserProject> UserProjects { get;set; }
public RecruitmentDbContext()
: base("RecruitmentDB")
{
}}
我在这个 page 上看到这足以创建具有多对多关系的新 table。这是真的,因为一切正常。但我想向这个自动生成的 table 添加新列,名称为 "ApplicationStatus"。我需要这个专栏,因为我需要知道,对于当前用户来说,当前项目是否具有当前状态:)
所以总结一下:
我想要一个 table 与多对多关系和名为 ApplicationStatus 的附加列。是否可以在自动生成的 table 上获得它,或者我应该以某种方式手动创建它?
是的,这是可能的。这应该有所帮助。
Entity Framework : Customized Join Table in a Many to Many Relationship
我正在尝试首先在代码中创建具有多对多关系的新 table。 我有两个 table:用户和项目。现在,我想为这两个 table 建立多对多关系。所以我做以下事情:
public class Project
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string PositionCount { get; set; }
public bool IsActive { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public class User : IdentityUser<Guid, UserLogin, UserRole, UserClaim>
{
public override Guid Id { get; set; }
public string Tags { get; set; }
public User()
{
Id = Guid.NewGuid();
}
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, Guid> 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 virtual ICollection<Project> Projects { get; set; }
}
public class RecruitmentDbContext : IdentityDbContext<User, MyRole, Guid, UserLogin, UserRole, UserClaim>
{
public DbSet<Project> Projects { get; set; }
public DbSet<UserProject> UserProjects { get;set; }
public RecruitmentDbContext()
: base("RecruitmentDB")
{
}}
我在这个 page 上看到这足以创建具有多对多关系的新 table。这是真的,因为一切正常。但我想向这个自动生成的 table 添加新列,名称为 "ApplicationStatus"。我需要这个专栏,因为我需要知道,对于当前用户来说,当前项目是否具有当前状态:)
所以总结一下: 我想要一个 table 与多对多关系和名为 ApplicationStatus 的附加列。是否可以在自动生成的 table 上获得它,或者我应该以某种方式手动创建它?
是的,这是可能的。这应该有所帮助。
Entity Framework : Customized Join Table in a Many to Many Relationship