Entity Framework - Rename ForeignKey

1

I have the classes:

[Table("Editora")]
    public class Editora
    {
        [Key]
        public int EditoraID { get; set; }

        public string nome { get; set; }

        public List<Livro> livros { get; set; }

        public List<Telefone> telefone { get; set; } 
    }

public class Telefone
    {
        public int TelefoneID { get; set; }

        public string fixo { get; set; }

        public string cel { get; set; }
    }

I'm studying code first, I did the update-database. A foreign key to the Phone was created because there is a Phone List property in Editora. The name of this foreign key was created with the default name: "Editora_EditoraID", I would like to rename it.

    
asked by anonymous 13.06.2017 / 01:07

2 answers

1

As far as I could understand, you did the same schema in your question and migration generated the field names by their default form ( migration has its default form, if do not put the settings the code generates the names including the relations of their way ), this happens for lack of initial configuration, the important thing is always to do the right thing, and put the relationships as you wish, names and ways not to have these problems, although in your case it is not a problem just do what you need so that it creates a new% update migration to solve the problem.

Changes must be made to the classes and re-run Migration

[Table("Editora")]
public class Editora
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int EditoraID { get; set; }
    public string Nome { get; set; }

    public virtual List<Livro> Livros { get; set; }
    public virtual List<Telefone> Telefones { get; set; }
}
[Table("Telefone")]
public class Telefone
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int TelefoneID { get; set; }
    public string Fixo { get; set; }
    public string Cel { get; set; }

    public int EditoraID { get; set; }

    [ForeignKey("EditoraID")]
    public virtual Editora Editora { get; set; }
}
[Table("Livro")]
public class Livro
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int LivroID { get; set; }
    public string Titulo { get; set; }

    public int EditoraID { get; set; }
    [ForeignKey("EditoraID")]
    public virtual Editora Editora { get; set; }
}

Ready with this the configurations can be updated and with an ideal nomenclature, like:

Retype the commands:

add-migration update3

and then

update-database

Tip: Any changes to your classes that will generate new settings on your base will repeat the commands Fluent API and then add-migration as new settings or changes will be applied.

    
15.06.2017 / 01:09
1
___ erkimt ___ Entity Framework - Rename ForeignKey ______ qstntxt ___

I have the classes:

public class Telefone
{
    public int TelefoneID { get; set; }

    //Chave estrangeira para tabela editora
    public int EditoraID { get; set;}

    public string fixo { get; set; }

    public string cel { get; set; }

    //Propriedade que representará a relação entre as tabelas
    [ForeignKey("EditoraID")]
    public virtual Editora Editora { get; set; }
}

I'm studying code first, I did the update-database. A foreign key to the Phone was created because there is a Phone List property in Editora. The name of this foreign key was created with the default name: "Editora_EditoraID", I would like to rename it.

    
______ azszpr212996 ___

As far as I could understand, you did the same schema in your question and %code% generated the field names by their default form ( migration has its default form, if do not put the settings the code generates the names including the relations of their way ), this happens for lack of initial configuration, the important thing is always to do the right thing, and put the relationships as you wish, names and ways not to have these problems, although in your case it is not a problem just do what you need so that it creates a new% update %code% to solve the problem.

Changes must be made to the classes and re-run %code%

public class Telefone
{
    public int TelefoneID { get; set; }

    //Chave estrangeira para tabela editora
    public int EditoraID { get; set;}

    public string fixo { get; set; }

    public string cel { get; set; }

    //Propriedade que representará a relação entre as tabelas
    [ForeignKey("EditoraID")]
    public virtual Editora Editora { get; set; }
}
%pre% %pre%

Ready with this the configurations can be updated and with an ideal nomenclature, like:

Retype the commands:

%pre%

and then

%pre%

Tip: Any changes to your classes that will generate new settings on your base will repeat the commands %code% and then %code% as new settings or changes will be applied.

    
______ ___ azszpr212397

I do not know if I understand correctly, but I think you are referring to the foreign key to the Entity automatically creates the database (usually: exemplo_ID). If that is the case you can set it manually

1- Create a property that will represent your foreign key:

%pre%

You may need to use the "Update-Database -Force" command to ensure that the update will be made.

obs: Also search for fluent api to do this mapping link

    
___
13.06.2017 / 02:15