Answered How to map Relationship name in EF 4.0 Code First

  • Saturday, March 03, 2012 4:25 PM
     
     

    Hi, 

    How can I change a relationship name between two tables in entity framework (Code First)


    • Edited by RainClick Saturday, March 03, 2012 4:26 PM
    •  

All Replies

  • Saturday, March 03, 2012 8:12 PM
     
     

    How are you creating your Code First model? If you are doing pure Code First you would not have a designer as shown in your question.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Sunday, March 04, 2012 6:16 AM
     
     

    Dear Fernando,

    Yes I'm doing pure Code First, And that snapshot is from SQL Manager just to show where I need my requested answer take an effect!

  • Monday, March 05, 2012 7:40 PM
     
     

      

    I thought I read somewhere that this could be done but can't find it. Sorry.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Monday, March 05, 2012 9:44 PM
    Moderator
     
     

    This is from the documenation: http://msdn.microsoft.com/en-us/library/hh295843(v=vs.103).aspx 

    If you want to specify the join table name and the names of the columns in the table you need to do additional configuration by using the Map method. The following code generates the CourseInstructor table with CourseID and InstructorID columns.

    modelBuilder.Entity<Course>()
        .HasMany(t => t.Instructors)
        .WithMany(t => t.Courses)
        .Map(m =>
        {
            m.ToTable("CourseInstructor");
            m.MapLeftKey("CourseID");
            m.MapRightKey("InstructorID");
        });

    You can also specify your schema name as the second paramater in ToTable method. For example,  ToTable("YourTableName", YourDbSchema");

    Please, let me know if that worked for you.

    thank you,

    Julia


    This posting is provided "AS IS" with no warranties, and confers no rights.

  • Tuesday, March 06, 2012 3:18 AM
     
     

    Hi Julia;

    I believe that @RainClick wants to give the relationship name something other then what SQL gives as a default and not to rename the table names. Can you help with that? I was looking around and could not find and links.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Tuesday, March 06, 2012 5:13 AM
     
     

    Dear Julia,

    As Fernando said, I need to change the "RELATIONSHIP NAME" ..

  • Tuesday, March 06, 2012 6:33 AM
    Moderator
     
     

    Sorry, I misunderstood your question.

    I don't think it is currently possible to configure the name of the Relationship.

    I will ask someone on the product team to reply to you.

    thank you,

    Julia


    This posting is provided "AS IS" with no warranties, and confers no rights.



  • Tuesday, March 06, 2012 6:07 PM
     
     

    RainClick,

    You can do it if you use Code First Migrations to create your database. The DbMigration.AddForeignKey methods take an optional name parameter.

    Cheers,

    Andrew.

  • Friday, March 09, 2012 1:53 AM
    Moderator
     
     

    Hi RainClick,

    Welcome to MSDN Forum.

    Does @Julia's solution can help you to solve the issue? I look forward to hearing from you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

  • Friday, March 09, 2012 6:59 AM
     
     

    Hi Allen,

    No it doesn't. 

  • Monday, March 12, 2012 6:41 AM
    Moderator
     
     

    Hi RainClick,

    I can't find a method to realize it. I will consult product team to make sure whether EF support it, and I will come back as soon as possible.

    Thanks for your understanding.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

  • Thursday, March 15, 2012 1:47 AM
    Moderator
     
     Answered

    Hi RainClick,

    I have consulted product team, and I'm afraid there's no way to do that now.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us