ef6 database migrations change table name - entity-framework

I am using EF6 code first and database migrations to keep my new database up do date.
I wanted to change the name of one of the database tables from "contacts" to "contact".
So in EF I change the name of the class and in the customised DBContext class I rename Contacts to Contact so it is now showing;
public DbSet<Contact> Contact { get; set; }
However I run the database migrations with Update-Database -Verbose -Force and no change is made.
To find out what is going on I put a new field in, and it tries to update the Contacts table rather than Contact which it needs to create.
So how do I fix this?

Try removing Pluralizing:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

I found the answer which is to use the following attribute in from of the class declaration;
[Table("Contact")]

Related

Entity Framework code-first migrations using SQL views

I have decided to use code first migrations. I did not use them before. I have, in the past just created the models and then added the tables as needed to the database. I use several SQL Views in my project. I create the SQL View and then create a model for it. It works fine, however when I do migrations it treats them like tables and adds them to the database as a table.
I could use the
modelBuilder.Ignore<AspNetUsers>();
for the view, but is there any way to actually create the SQL view from the model? Below is one of my SQLView entities
[NotMapped]
public IDbSet<StockReportView> StockReportView { get; set; }
If not then I will just continue to create the SQL views and create the model and then add it to the OnModelCreating as modelBuilder.Ignore<StockReportView>();
Thanks for all your help.
UPDATE:
I found a question that was closed but had some answers. I Implemented what they said. Which was:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace SomeProject.Data
{
[Table("SomeView")]
public class SomeView
{
[Key]
public int NameID { get; set; }
public string Name { get; set; }
}
}
This deleted all of the tables it created That were SQL Views, But did not create the SQLViews.

Change Entity Framework Migrations table name

I am using Entity Framework 7 and I would like to rename
"__EFMigrationsHistory"
table name to
"__Migrations".
So I have the following on my context:
protected override void OnModelCreating(ModelBuilder builder) {
base.OnModelCreating(builder);
builder
.Entity<HistoryRow>()
.ToTable("__Migrations")
.HasKey(x => x.MigrationId);
// Remaining configuration
}
I am able to create the migration but when I apply it to the database I get:
There is already an object named 'PK_HistoryRow' in the database.
Could not create constraint or index. See previous errors.
I have been trying a few options but I always end with a problem.
Does anyone knows the best way to do this?

EDMX is overwriting Key data annotations

This applies to EF 5 and database first modeling. My model was built useing the EF generator from an existing DB.
I'm using the [Key] data annotation in my model classes because the primary key fields have names that are not in line with EF conventions.
Everything works, but when I open the root EDMX files, the model classes are updated and any manual changes I had made are lost.
Should I be making my changes in a different manner?
You could update your T4 template to add in the data annotation for you on primary keys?
if (simpleProperties.Any())
{
foreach (var edmProperty in simpleProperties)
{
if (ef.IsKey(edmProperty)){
#>
[Key]
<# } #>
the solution that worked for me was to overide the EF convention in OnModelCreating method in the context class.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<abk_Bookings>()
.HasKey(e => e.booking_number);}

EF Code First Database Migrations - how to exclude a column from being migrated?

I have a class from which I use Database Migrations to update a corresponding table.
However there is a field I want to put into the classes that should not get migrated to the database.
Is there an attribute I can use to do this?
Something like
[IgnoreColumn]
public bool Selected { get; set; }
Ignore ONLY for database updates.
Yes, you can ignore properties:
modelBuilder.Entity<YourEntity>().Ignore(p=>p.foobar);

EF Code-First cached bad table name?

I have a console application that is using code first Entity Framework 4.3.1. I created a class, a DbContext, a DbSet, and I have a database connection.
The issue is that I mispelled the table name and the program threw an error. I changed the name in the DbSet and the system keeps throwing the same error that has the old name.
Example:
public DbSet<SHIPPER> SHIPPERs { get; set; }
This could not find the SHIPPERs table in SQL server. No problem. I changed it to
public DbSet<SHIPPER> SHIPPER { get; set; }
and I get Invalid object name 'dbo.SHIPPERs'."
I did a search in Visual Studio for SHIPPERs and nothing was returned. What am I missing?
Note: I created another DbSet for a different table and that works.
The problem was with pluralization. Add the following code inside of your DbContext class to fix the issue:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}