3
I have the following classes where I want to make a relationship 1 to 1:
User:
[Table("Usuario")]
public class Usuario
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[EmailAddress]
[StringLength(150)]
[DataType(DataType.EmailAddress)]
[Index("unq_Usuario_Login", IsUnique = true)]
public string Login { get; set; }
[Required]
[StringLength(30)]
public string Nome { get; set; }
[StringLength(50)]
[DataType(DataType.Password)]
public string Senha { get; set; }
[InverseProperty("Usuario")]
public virtual UsuarioConfiguracao Configuracoes { get; set; }
[InverseProperty("Usuario")]
public virtual ICollection<UsuarioDepartamento> Departamentos { get; set; }
}
User Settings:
[Table("UsuarioConfiguracao")]
public class UsuarioConfiguracao
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public int UsuarioId { get; set; }
[ForeignKey("UsuarioId")]
public virtual Usuario Usuario { get; set; }
[Required, DefaultValue(false)]
public bool EMailDeAtendimento { get; set; }
[Required, DefaultValue(false)]
public bool EMailDeArquivoPublicado { get; set; }
[Required, DefaultValue(false)]
public bool SmsDeAtendimento { get; set; }
[Required, DefaultValue(false)]
public bool SmsDeArquivoPublicado { get; set; }
...
}
When rotating the enable-migrations
get the following error message:
Unable to determine the main end of an Association between the types 'Domain.User' and 'Domain.User configuration'. The main end of this Association must be explicitly configured using either the Relationship Fluent API or data Annotations..
I think I’ve seen similar questions here at Sopt but I haven’t found.
And I’d like to know how to deal with Attributes
and why this occurs?
Editing
When placing the attribute Required
on the property Configuracoes
class Usuario
my mistake has changed:
[Required]
[InverseProperty("Usuario")]
public virtual UsuarioConfiguracao Configuracoes { get; set; }
The Foreignkeyattribute on Property 'Usuario' on type 'Domain.Usuarioconfiguracao' is not Valid. The Foreign key name 'Usuarioid' was not found on the dependent type 'Dominio.Usuario'. The Name value should be a comma separated list of Foreign key Property Names.
I found it strange but changed in my User class:
[ForeignKey("Id")]
public virtual Usuario Usuario { get; set; }
And it worked the addition of Migrations. Is that right? Because he didn’t accept the UsuarioId
? It should not be the property of the class in which the attribute is itself, which in this case would refer to Key
of the class referred to in?
Dude, I don’t think you need to declare the User twice like you’re doing... take that User Foreignkey note to see if it works. Maybe that’s what.
– Érik Thiago
That solved your problem ?
– Érik Thiago
Because this Id you are pulling as a foreign key is from your User table and not from the User table configuration, that is, the concept of inheritance of the answer below. Got it ?
– Érik Thiago
Calm man. Needing we are there !
– Érik Thiago