Generated identifier too long for constraint name

I am just starting out with the Entity Framework (4.3.1) and creating the first code model with TPT inheritance using the MySQL EF provider (6.5.4). When I try to create a database structure, I get a MySQLException "Identifier name 'LongClassNameOne_TypeConstraint_From_ClassName2s_To_LongClassNameOnes' is too long" (This is the relation that binds the table of the derived class to the table of the parent class.)

  at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int32& insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int32& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() at MySql.Data.MySqlClient.MySqlScript.Execute() at MySql.Data.MySqlClient.MySqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection) at System.Data.Objects.ObjectContext.CreateDatabase() at System.Data.Entity.Internal.DatabaseOperations.Create(ObjectContext objectContext) at System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext) at System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext) at System.Data.Entity.Database.Create() at System.Data.Entity.DropCreateDatabaseAlways`1.InitializeDatabase(TContext context) at System.Data.Entity.Database.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c) at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6() at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Entity.Internal.InternalContext.ForceOSpaceLoadingForKnownEntityTypes() at System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext() ... 

How can I control this name to make it shorter or make it trim to a 64-character name limit or something in that direction?

+7
source share
2 answers

You need to use a shorter table name, as there is no way to configure the constraint names. Please note: you do not need to change class names for this. You can use the Table attribute or use the ToTable () method in OnModelCreating.

+5
source

Extend my comment earlier ...

If you can just change the name of the table - then go with the display in OnModelCreating - as @Pawel suggested , which is probably the easiest solution for everyone.

However, if you only want to change the name of the relationship,

... by providing a custom SqlGenerator (i.e. SqlServerMigrationSqlGenerator ) in Configuration() , you can micro-control the actual sql generated if necessary (and this could be a general automatic solution in some general case). eg

 public class MySqlGenerator : SqlServerMigrationSqlGenerator { protected override void Generate(AddForeignKeyOperation addForeignKeyOperation) { if (addForeignKeyOperation.Name == "LongClassNameOne_TypeConstraint_From_ClassName2s_To_LongClassNameOnes") addForeignKeyOperation.Name = "MyCustomFKName"; // addForeignKeyOperation.Name = "Test" + addForeignKeyOperation.Name; base.Generate(addForeignKeyOperation); } } 

... or something in this direction (you need to combine, find the correct name - or compare Name Length and shorten it where necessary. And in your configuration (file created by migrations) ...

 public Configuration() { AutomaticMigrationsEnabled = false; SetSqlGenerator("MySQL provider??", new MySqlGenerator()); // SetSqlGenerator("System.Data.SqlClient", new MySqlGenerator()); } 

(note: I don't know what the name of the MySQL provider is)

... this should change the FK relationship - and as fast as you could check it, it works fine, since the name of the relationship is not actually used in the model from C # (just the db name usually).

+5
source

All Articles