Saturday, 15 March 2014

sql server - Entity Framework primary key constraint -


/ Code>, where TableName is in a plural form, even if the actual name of the table is a unique form

For example, I have dbo Employee is a table named . Still the primary key is represented by EF 6 PK_dbo.Employees .

Is there a way to change something like PK_TableName , where tablename is in a unique form?

Update: Thanks for the reply. I did not mean to rename the primary name column name What I like to do is change the name of major obstacles in the generated database, which is requested by a database person in our team. Sorry I did not clarify this in my first post. I have seen some discussions in this post: Though no simple solution has been identified so far.

You can do this:

  public class PKNameGenerator: SqlServerMigrationSqlGenerator {static readonly string PREFIX =" PK "; Secure Override Zero (CreateTableOperation createTableOperation) {createTableOperation.PrimaryKey.Name = GetPkName (createTableOperation.Name); Base.Generate (createTableOperation); } Secure Override Generates Zero (AddPrimaryKeyOperation addPrimaryKeyOperation) {addPrimaryKeyOperation.Name = GetPkName (AddPreference Operation. Table); Base.Generate (addPrimaryKeyOperation); } Protected override zero (Operation dropprimary operation of dropprimaries) {dropprinteraction operation .name = GetPkName (dropprimery operation.table); Base.Generate (dropPrimaryKeyOperation); } // prefix + name without schema string GetPkName (string tagname) {return PREFIX + tableName.Substring (tableName.IndexOf ('.') + 1); }}  

And then you have to register it like this:

  public class datacentex configuration: DB configuration {PUBLIC DATA CONTENT CONFIGURATION () {SetMigrationSqlGenerator (SqlProviderServices.ProviderInvariantName, () => New PKNameGenerator ()); }}  

Be sure to keep the above class or use DbConfigurationTypeAttribute

  [DBC configuration type (typef (custom DBC configuration)] Public class Your FITS: DBCTXST  

Source:

More information: section SQL Generation

Using the EF core, you can do it like this:

  class MyContext: DbContext {public DbSet & Lt; Blog & gt; Receive blogs { Set; } Secure Override Zero OnModelCreating (Modelbuilder Modalbuilder) {modelBuilder.Entity & lt; Blog & gt; () .HasKey (b = & gt; b.BlogId) .HasName ("PrimaryKey_BlogId"); }} Public Square Blog {Public Ink BlogId {get; Set; } Public String Url {get; Set; }}  

Source:


No comments:

Post a Comment