创建实体模型多对多 CodeFirst Ef6
Create entity model Many-To-Many CodeFirst Ef6
我希望 Db 使用代码优先
我做了什么:
public class Responses
{
public int UserId { get; set; }
public virtual AppUser AppUser { get; set; }
public int QuestionId { get; set; }
public virtual Question Question { get; set; }
public string Answer { get; set; }
}
public class Question
{
public int idQuestion { get; set; }
public string TextQuestion { get; set; }
public ICollection<Responses> Responses { get; set; }
}
public class AppUser : IdentityUser<int, AppUserLogin, AppUserRole, IdentityUserClaimBase>, ICRMRepository, IEditableEntity, IEntityBase
{
public int idUser {get;set;}
public ICollection<Responses> Responses { get; set; }
}
接下来我转到 DbContext:
modelBuilder.Entity<Responses>()
.HasKey(x => new {x.UserId, x.QuestionId});
modelBuilder.Entity<Responses>()
.HasOne(x=>x.User)
无法解析符号“HasOne”
如果我想得到这样的数据库,我应该怎么做?
如何配置我与流利 API 的关联?或者有没有更好的方法来创建关联 table?
更新
更改响应 class,将 AppUser 属性 替换为用户
public class Responses
{
public int UserId { get; set; }
public virtual AppUser User{ get; set; }
........
}
并将此代码用于 Db 上下文
modelBuilder.Entity<Response>(entity =>
{
entity.HasOne(d => User)
.WithMany(p => p.Responses)
.HasForeignKey(d => d.UserId);
entity.HasOne(d => d.Question)
.WithMany(p => p.Responses)
.HasForeignKey(d => d.QuestionId;
});
我能解决:
public class AppUser : IdentityUser<int, AppUserLogin, AppUserRole, IdentityUserClaimBase>, ICRMRepository, IEditableEntity, IEntityBase
{
public ICollection<Response> Responses { get; set; }
}
public class Question
{
public int Id { get; set; }
public string TextQuestion { get; set; }
public ICollection<Response> Responses { get; set; }
}
public class Response
{
public int Id { get; set; }
***public int AppUserId { get; set; }***
public int QuestionId { get; set; }
public virtual AppUser AppUser { get; set; }
public virtual Question Question { get; set; }
public string Answer { get; set; }
}
数据库上下文:
public DbSet<Question> Questions { get; set; }
public DbSet<Response> Responses { get; set; }
EF6明白我想要什么
我希望 Db 使用代码优先
我做了什么:
public class Responses
{
public int UserId { get; set; }
public virtual AppUser AppUser { get; set; }
public int QuestionId { get; set; }
public virtual Question Question { get; set; }
public string Answer { get; set; }
}
public class Question
{
public int idQuestion { get; set; }
public string TextQuestion { get; set; }
public ICollection<Responses> Responses { get; set; }
}
public class AppUser : IdentityUser<int, AppUserLogin, AppUserRole, IdentityUserClaimBase>, ICRMRepository, IEditableEntity, IEntityBase
{
public int idUser {get;set;}
public ICollection<Responses> Responses { get; set; }
}
接下来我转到 DbContext:
modelBuilder.Entity<Responses>()
.HasKey(x => new {x.UserId, x.QuestionId});
modelBuilder.Entity<Responses>()
.HasOne(x=>x.User)
无法解析符号“HasOne”
如果我想得到这样的数据库,我应该怎么做?
如何配置我与流利 API 的关联?或者有没有更好的方法来创建关联 table?
更新
更改响应 class,将 AppUser 属性 替换为用户
public class Responses
{
public int UserId { get; set; }
public virtual AppUser User{ get; set; }
........
}
并将此代码用于 Db 上下文
modelBuilder.Entity<Response>(entity =>
{
entity.HasOne(d => User)
.WithMany(p => p.Responses)
.HasForeignKey(d => d.UserId);
entity.HasOne(d => d.Question)
.WithMany(p => p.Responses)
.HasForeignKey(d => d.QuestionId;
});
我能解决:
public class AppUser : IdentityUser<int, AppUserLogin, AppUserRole, IdentityUserClaimBase>, ICRMRepository, IEditableEntity, IEntityBase
{
public ICollection<Response> Responses { get; set; }
}
public class Question
{
public int Id { get; set; }
public string TextQuestion { get; set; }
public ICollection<Response> Responses { get; set; }
}
public class Response
{
public int Id { get; set; }
***public int AppUserId { get; set; }***
public int QuestionId { get; set; }
public virtual AppUser AppUser { get; set; }
public virtual Question Question { get; set; }
public string Answer { get; set; }
}
数据库上下文:
public DbSet<Question> Questions { get; set; }
public DbSet<Response> Responses { get; set; }
EF6明白我想要什么