Entity Framework Code First - Changing a Table Name

.net c# entity-framework

Question

One of my tables that was created using Entity Framework code needs to have its name changed first.

The database is already built, but I now want to modify the name. Each and every mention to the"Metadatas" desk to"Metadata" in my endeavor. But "Metadatas" is still the name of the database table that is being created. The database has been deleted and recreated, but that doesn't appear to help either. Using a TableAttribute does not either. What should I do now?

Thanks.

[Table("Metadata")]
public class Metadata 
{
    [Required, Key]
    public int MetadataId { get; set; }

    [Required, ScaffoldColumn(false)]
    public int DocumentId { get; set; }

    [Required, StringLength(250), DataType(DataType.Text)]
    public string Title { get; set; }

...

1
27
2/9/2014 7:45:03 AM

Accepted Answer

Here, you have two choices:

Statistical Notes:

//Changing database table name to Metadata
[Table("Metadata")]
public class Metadata 
{
  [Required, Key]
  public int MetadataId { get; set; }

  [Required, ScaffoldColumn(false)]
  public int DocumentId { get; set; }

  [Required, StringLength(250), DataType(DataType.Text)]
  public string Title { get; set; 
}

Or, there is Fluent Api:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
  //Changing Database table name to Metadata
  modelBuilder.Entity<Metadata>()
      .ToTable("Metadata");
}

If you want to keep your Domain Model clean, using the Fluent Api is the best choice.

Just to add to this, you can override EF's ability to do this with the line of code below if you only want to eliminate the pluralization of your table names.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
51
12/29/2014 7:31:46 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow