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?
Related
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")]
I wonder if anyone out there can help me with the following problem...
I have a created a EF code first database (sqlexpress) that uses Identity v2 -I can see within that database all my models, including the Identity related tables starting with Identity* (ie. IdentityUserClaims, IdentityUserLogins, IdentityUserRoles IdentityRoles & IdentityUsers).
I then create a brand new MVC project as well as adding the same connection string. When I register a new user -for some reason it goes off and creates Identity v2 tables starting with AspNet* (ie. AspNetUserClaims, AspNetUserLogins, AspNetUserRoles AspNetRoles & AspNetUsers).
[I am using... EF 6.1.1, Identity 2.1, MVC 5.2]
Why does the Identity naming schema differ between EF and AspNet ? how do I make MVC use the EF schema Identity* ?
You can override table names by [Table("MyUsers")] attribute on top of your classes:
[Table("WhateverILikeUsers")]
public class ApplicationUser : IdentityUser
{
}
Or you can rename tables in DbContext:
public class MyContext : IdentityDbContext<ApplicationUser>
{
// other stuff
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ApplicationUser>().ToTable("HelloIdentity");
}
}
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);}
I'm trying to add an EntityObject to my database by calling AddToLeistungGruppe.
LeistungGruppe in this case is my Table with Primary_Key LeistungGruppe_ID with Identity true and Identity increment 1 and seed 1.
I search a lot for this issue and alot of People got he same error.
They were told to simply set StoreGeneratedPattern to Identity and this would solve the Problem.
I tried it out and still got the same issue.
I'm new to the Entity Framework and have no idea about how to solve this Problem.
Somehow i think the model isn't updated probably because even if i Switch around These Settings I'm getting the same error over and over again.
Every help is appreciated.
You are trying to save an object to the database with an explicit ID set by you while the database is expecting to generate that value itself. That is the LeistungGruppe_ID property in your object is set to something other than 0 and it is not identified to the EF framework as an identity field. If you want the Id to be generated by the database as your post suggests, then the corresponding property in the Object should be decorated with the [Key] attribute.
If you are using the Fluent API then you should have something like this in your DBContext:
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
modelBuilder.Entity<LeistungGruppe>().Property(x => x.LeistungGruppe_ID).StoreGeneratedPattern = StoreGeneratedPattern.Identity;
base.OnModelCreating(modelBuilder);
}
I already have few migrations, and now I am adding another migration, but this time I want to add seed to it. I tried adding this near my Up() and Down() methods:
protected override void Seed(ScykDb context)
{
}
But my compiler says I cannot do that. How can I add seed to my migration?
Seed is not available per migration, only at the DbContext level.
You can easily get around it by adding a call to Sql() in your Up() method:
Sql("insert into ponies (col1, col2.....");