所以,我收到错误消息"实体类型' System.Collections.Generic.IEnumerable`1 [SomeModel]'提供参数' clrType'必须是参考类型。 "当我尝试添加迁移时。我不知道这里有什么问题。有人可以帮帮忙吗? 不知道UserRequest类有什么问题。
我看到了一些类似的问题,但继承控制器和类似问题存在问题。
谢谢!欢迎任何帮助!
public class UserRequest
{
[ScaffoldColumn(false)]
[System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int UserAccountId { get; set; }
public RequestCode RCode { get; set; }
public string CodeText { get; set; }
}
public class UserAccount
{
[ScaffoldColumn(false)]
[System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int UserAccountId { get; set; }
[Required(ErrorMessage = "First Name is required.")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Second Name is required.")]
public string LastName { get; set; }
[Required(ErrorMessage = "Email is required.")]
[RegularExpression(@"^([\w-\.]+)@((\[[0-9]{1,3]\.)|(([\w-]+\.)+))([a-zA-
Z{2,4}|[0-9]{1,3})(\]?)$",
ErrorMessage = "Please enter valid email.")]
public string Email { get; set; }
[Required(ErrorMessage = "Username is required.")]
public string UserName { get; set; }
[Required(ErrorMessage = "Password is required.")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Compare("Password", ErrorMessage = "Please confirm your password.")]
[DataType(DataType.Password)]
public string ConfirmPassword { get; set; }
public Role MyRole { get; set; }
public UserAccount()
{
MyRole = Role.Customer;
}
}
public class Administrator : UserAccount {
public IEnumerable<UserRequest> ReceievedRequests { get; set; }
}
public class Customer : UserAccount
{
public int Spent { get; set; }
public ShoppingCart Cart { get; set; }
public virtual IEnumerable<UserRequest> UserRequests { get; set; }
}
DataAccessLayer
public class TalonContext : DbContext
{
public TalonContext() { }
public TalonContext(DbContextOptions<TalonContext> options) :
base(options) { }
public DbSet<UserAccount> Users { get; set; }
public DbSet<UserRequest> Requests { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Administrator>()
.HasMany(r => r.ReceievedRequests);
//modelBuilder.Entity<Customer>()
// .HasOne(r => r.UserRequests);
modelBuilder.Entity<Customer>() <--fixed
.HasMany(r => r.UserRequests);
modelBuilder.Entity<UserRequest>()
.HasAlternateKey(k => k.UserAccountId);
}
}