When i try to add migration, this fails.
Im using this model as request from users to administrator, so there are relationships many to one, and one to many. (if that is important, because i see it mess with this IEnumerable type).
I just have no idea what is wrong with this. Really i do not.
Ask with question-comment, which part of code you want to see with this model which makes problem, i tried to explain you what is going on,
Exception message: The entity type 'System.Collections.Generic.IEnumerable`1[SomeModel]' provided for the argument 'clrType' must be a reference type.
```c#
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; } //i wasnt sure could this make problem, so i just put it
under comment, because it was ENUM type, put again, it fails
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
}
public class Customer : UserAccount
{
public int Spent { get; set; }
public ShoppingCart Cart { get; set; }
public virtual IEnumerable<UserRequest> UserRequests { get; set; }
}
ContextClass
public DbSet
public DbSet
public DbSet
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity
.HasMany(r => r.ReceievedRequests);
modelBuilder.Entity<Customer>()
.HasOne(r => r.UserRequests);
modelBuilder.Entity<UserRequest>()
.HasAlternateKey(k => k.UserAccountId);
}
```
EF Core version: 2.0.6
Database Provider: Microsoft.EntityFrameworkCore.SqlServer Version="2.0.2"
Operating system: win 10
IDE: VS 2017
There are no relationship in above entity. You need to share all the model classes which when added to EF model causes the issue. Otherwise there is nothing much for us to investigate here.
I updated it ... Had many tries and searches on internet, but still cant solve this ... I dont understand what im doing wrong ...
Error is in this line
modelBuilder.Entity<Customer>()
.HasOne(r => r.UserRequests);
HasOne is used to configure reference navigation. You are using it on a collection navigation. Hence the error message that wrong type used for reference message. Use HasMany instead.
Triage: Exception message is wrong--it's not because it's not a reference type, it's because it is not a reference navigation.
Most helpful comment
Error is in this line
HasOneis used to configure reference navigation. You are using it on a collection navigation. Hence the error message that wrong type used for reference message. UseHasManyinstead.