Ticket T486359
Visible to All Users

Code migration unexpectedly tries to rename table

created 8 years ago (modified 8 years ago)

This could be an Entity Framework Issue so I will cross post on Stack Overflow.
I want to implement a change log as advised in https://www.devexpress.com/Support/Center/Question/Details/T474899">T474899
I am using the security system generated by the new solution wizard
I have defined some business objects to store the change log information.

One of these objects stores a link to the user

public virtual User User { get; set; }

On generating the code migration I am surprised to see the Up() method add the following

C#
RenameTable(name: "dbo.UserRoles", newName: "RoleUsers"); DropPrimaryKey("dbo.RoleUsers"); AddPrimaryKey("dbo.RoleUsers", new[] { "Role_ID", "User_ID" });
Comments (1)

Answers approved by DevExpress Support

created 8 years ago (modified 8 years ago)

Hello Kirsten,

We also do not see any DevExpress XAF specifics here as this code is fully generated by EF CodeFirst Migrations. I hope you will receive answers from SO or Microsoft support.
I am not 100% sure, but it is likely caused by some built-in convention like PluralizingTableNameConvention described at https://msdn.microsoft.com/en-us/library/jj679962(v=vs.113).aspx
We and all the XAF community would greatly appreciate it if you update this thread once you find the cause of this behavior or a way to customize it. Thanks.

    Show previous comments (3)

      Thanks Dennis, does the old database diagram pasted on Stack Overflow look right to you?

      Dennis Garavsky (DevExpress) 8 years ago

        My diagram for the security classes looks a bit different, though this may just be due to the intentional omission of certain tables on your side.

        BTW, there is a small correction to the description of your SO thread: >>I think am using Standard Authentication with Allow/Deny in the XAF Solutions Wizard.
        Since you use the User and Role classes from the DevExpress.Persistent.BaseImpl.EF namespace, you are NOT actually using the new Allow/Deny strategy, which was added in v16.1 and which is associated with the PermissionPolicyXXX classes.

          seems to me that Code first can't "know" which of 2 names to use in a many to many table  (RoleUsers or UserRoles) unless there is fluent mapping in the OnModelCreating method

          Other Answers

          created 8 years ago (modified 8 years ago)

          This problem re-occurred for me when I tried adding a new Foreign Key to an existing table.

          The fact that the foreign key was to my staff table, and that my staff table contains a foreign key referencing the User table may have had something to do with things.

          The work around was to introduce the following code to explicitly tell EF the name of the joiner table.

          C#
          public class RoleConfiguration : EntityTypeConfiguration<Role> { public RoleConfiguration() { HasMany(x => x.Users).WithMany(y => y.Roles).Map(x => x.ToTable("UserRoles")); } } public class UserConfiguration : EntityTypeConfiguration<User> { public UserConfiguration() { HasMany(x => x.Roles).WithMany(y => y.Users).Map(x => x.ToTable("UserRoles")); } }
          C#
          protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Configurations.Add(new RoleConfiguration()); modelBuilder.Configurations.Add(new UserConfiguration()); }

          When I added the next migration  using Package Manager Console

          C#
          add-migration location5

          after commenting out the code I got

          C#
          public partial class roles3 : DbMigration { public override void Up() { //RenameTable(name: "dbo.RoleUsers", newName: "UserRoles"); } public override void Down() { // RenameTable(name: "dbo.UserRoles", newName: "RoleUsers"); } }

          when I ran

          C#
          update-database

          In Package Manager Console

          The program then behaved as expected.

            Comments (1)
            Dennis Garavsky (DevExpress) 8 years ago

              Thanks for taking the time to describe your final solution here for other XAF users.

              Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

              Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.