我希望資料庫使用代碼優先 
我做了什么:
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”
如果我想得到這樣的資料庫,我該怎么辦?
如何配置我與 fluent API 的關聯?或者有沒有更好的方法來創建關聯表?
UPD

uj5u.com熱心網友回復:
更改回應類,用用戶替換 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;
});
uj5u.com熱心網友回復:
我可以解決它:
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 明白我想要什么
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/327009.html
標籤:C# asp.net-mvc 实体框架 有效代码优先
