Entity Framework - rename Foreignkey

Asked

Viewed 185 times

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 the code first, I did the update-database. A foreign key to the Telephone has been created because there is a property of type Telephone List in Publisher. The name of this foreign key was created with the default name: "Editora_editoraid", I would like to rename it.

  • Rename exactly what? Cite a clear example, that is, what happened and you want me to do!

  • Like I said, rename foreignKey. Look at my Publisher class, there is a Phone type property which has created a foreign key with the default name, just like to rename it by the first code.

  • What was the name she put in the column of the bank? I mean, the default name ? I just want to understand!

  • No Phone was created FK: Editora_editoraid

  • I understand, you have not put the settings and already ran the correct Migration? and now you want to arrange the correct fields names?

  • I have already done Migration and update. I want to rename only this foreign key.

  • See my answers to rename it, the same need exist in the settings I did the whole process for you!

  • You got it @Heyjoe?

  • It seems to me that you do not understand? or understand?

Show 4 more comments

2 answers

1


Well, as far as I can make out, you did the same scheme as in your question and the migration generated the field names by their default form on their own (Migration has its default way of generating, if you do not put the settings the code generates the names including the relations of its mode), this happens for lack of initial configuration, the important thing is always to do the right, and put the relations as you want, names and ways to not have these problems, although in your case is not a problem is only to do what you need so that it generates a new migration update to solve the problem.

Minimal example:

Changes shall be made to the classes and rotate again to 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 settings can be updated and with an ideal nomenclature, as:

Do the commands again:

add-migration update3

and then

update-database

Observing: this is the minimum of settings I would recommend the mode Fluent API, links:

Tip: any changes in your classes that will generate new settings in your base repeat the commands add-migration and then update-database that new settings or changes will be applied.

  • Where exactly can I put the name I want to the foreign key in this code?

  • [ForeignKey("EditoraID")] is where you rename or name the Property ... @Heyjoe

  • Oh yes, I put any name of property which is actually FK and refer to it with [Foreignkey("<property>")]. But what would be this virtual property of Publisher?

  • This property is for you to have the possibility to get information from the Editor, since within the table Phone only has the identification, but, the data does not and for this property I can upload! @Heyjoe

1

I don’t know if I understand this correctly, but I believe you are referring to the foreign key that Entity automatically creates in the database (usually: example_ID), if this is the case you can set it manually.

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

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; }
}

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

Obs: Also search on Fluent api to make this mapping http://www.entityframeworktutorial.net/code-first/fluent-api-in-code-first.aspx

Browser other questions tagged

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