none
How to map Relationship name in EF 4.0 Code First RRS feed

  • Question

  • Hi, 

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


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

Answers

All replies

  • 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".

    Saturday, March 3, 2012 8:12 PM
  • 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!

    Sunday, March 4, 2012 6:16 AM
  •   

    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 5, 2012 7:40 PM
  • 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.

    Monday, March 5, 2012 9:44 PM
    Moderator
  • 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 6, 2012 3:18 AM
  • Dear Julia,

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

    Tuesday, March 6, 2012 5:13 AM
  • 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 6, 2012 6:33 AM
    Moderator
  • 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.

    Tuesday, March 6, 2012 6:07 PM
  • 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 9, 2012 1:53 AM
    Moderator
  • Hi Allen,

    No it doesn't. 

    Friday, March 9, 2012 6:59 AM
  • 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

    Monday, March 12, 2012 6:41 AM
    Moderator
  • 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

    Thursday, March 15, 2012 1:47 AM
    Moderator