Error While Creating Migration

Asked

Viewed 1,146 times

2

I am creating a user register in C#, but when including the Domain layer and I will create the Migration is happening the error below. I haven’t been able to identify what happens.

"No suitable constructor found for Entity type 'User'. The following Parameters could not be bound to properties of the Entity: 'cellphone', 'address'."

namespace AgendaWeb.Domain.Users
{
public class User
{
    public int Id { get; private set; }
    public string UserName { get; private set; }
    public string FullName { get; private set; }
    public string Cpf { get; private set; }
    public string Email { get; private set; }
    public string Phone { get; private set; }
    public string Cellphone { get; private set; }
    public string PhotoUrl { get; private set; }
    public string Password { get; private set; }
    public DateTime RegistrationDate { get; private set; }
    public DateTime? DateOfBirth { get; private set; }
    public string ProfileType { get; private set; }

    public Address Address { get; private set; }

    public User(string userName, string fullName, string cpf, string email, string phone, string cellPhone, string photoUrl, string password, DateTime registrationDate, DateTime? dateOfBirth, string profileType, Address address )
    {
        ValidateValues(userName, fullName, cpf, email, phone, cellPhone, photoUrl, password, registrationDate, dateOfBirth, profileType, address);
        SetProperties(userName, fullName, cpf, email, phone, cellPhone, photoUrl, password, registrationDate, dateOfBirth, profileType, address);

    }

    public void Update(string userName, string fullName, string cpf, string email, string phone, string cellPhone, string photoUrl, string password, DateTime registrationDate, DateTime? dateOfBirth, string profileType, Address address)
    {
        ValidateValues(userName, fullName, cpf, email, phone, cellPhone, photoUrl, password, registrationDate, dateOfBirth, profileType, address);
        SetProperties(userName, fullName, cpf, email, phone, cellPhone, photoUrl, password, registrationDate, dateOfBirth, profileType, address);

    }

    private void SetProperties(string userName, string fullName, string cpf, string email, string phone, string cellPhone, string photoUrl, string password, DateTime registrationDate, DateTime? dateOfBirth, string profileType, Address address)
    {
        UserName = userName;
        FullName = fullName;
        Cpf = cpf;
        Email = email;
        Phone = phone;
        Cellphone = cellPhone;
        PhotoUrl = photoUrl;
        Password = password;
        RegistrationDate = registrationDate;
        DateOfBirth = dateOfBirth;
        ProfileType = profileType;
        Address = address;
    }

    private static void ValidateValues(string userName, string fullName, string cpf, string email, string phone, string cellPhone, string photoUrl, string password, DateTime registrationDate, DateTime? dateOfBirth, string profileType, Address address)
    {
        DomainException.When(string.IsNullOrEmpty(userName), "O campo nome é obrigatório!");
    }
}

}

  • as Gustavo has already answered, you need to have a constructor without parameters for EF to create an instance, you can simply add public User(){} to his class

1 answer

3


If you are using the Entity Framework in . NET Full, you need to have a constructor without protected parameters.

If you are using the Entity Framework Core, it is possible to have constructors with parameters, but it is not possible to use complex types as parameters, so only primitive types can be used in the constructor.

In EF Core, you have two outputs: create a protected constructor without parameters, take out the complex types from the existing constructor.

Source: Manufacturer Entity Types - EF Core

Browser other questions tagged

You are not signed in. Login or sign up in order to post.