Often we might need to use Entity Framework Code First with an existing database.
The existing database may have a structure the allows "Table Per Hierarchy" inheritance.
Or we might start with an object model that looks like:
public partial class Person {
public int Id { get; set; }
public string Discriminator { get; set; }
public string Name { get; set; }
public Nullable<int> StudentTypeId { get; set; }
public virtual StudentType StudentType { get; set; }
}
public partial class StudentType {
public StudentType() {
this.People = new List<Person>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> People { get; set; }
}
We create the initial migration:
enable-migrations
add-migration Initial
The migration looks like:
public override void Up()
{
CreateTable(
"dbo.Person",
c => new
{
Id = c.Int(nullable: false, identity: true),
Discriminator = c.String(maxLength: 4000),
Name = c.String(maxLength: 4000),
StudentTypeId = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.StudentType", t => t.StudentTypeId)
.Index(t => t.StudentTypeId);
CreateTable(
"dbo.StudentType",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(maxLength: 4000),
})
.PrimaryKey(t => t.Id);
}
To generate this database we:
update-database
This results in a database that we could have generated like this.
create table Person(
Id int Identity(1,1) Primary key,
Discriminator nvarchar(4000) null,
StudentTypeId int null,
)
create table StudentType(
Id int Identity(1,1) Primary key,
Name nvarchar(4000) not null
)
alter table Person
add constraint StudentType_Person
foreign key (StudentTypeId)
references StudentType(Id)
We use this database in production for a while...
Now we want to add the concept of students that are different from just regular people.
Entity Framework provides three approaches for representing inheritance. In this case we choose the "Table Per Hierarchy" approach.
To implement this approach we modify our POCOs as follows:
public class Person {
public int Id { Get; set; }
public string Name { get; set }
}
public class Student : Person {
public virtual StudentType StudentType { get; set; }
public int? StudentTypeId { get; set; }
}
public class StudentType {
public StudentType() {
Students = new List<Student>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
Note:
Only Students have access to the StudentType property.
We don't specify the Discriminator property in our Person class. EF Code First sees that Student inherits from Person and will add a Discriminator column to the Person table for us.
Now we run:
add-migration Person_TPH
And we get this unexpected output.
public override void Up()
{
AddColumn("dbo.Person", "StudentType_Id", c => c.Int());
AlterColumn("dbo.Person", "Discriminator", c => c.String(nullable: false, maxLength: 128));
AddForeignKey("dbo.Person", "StudentType_Id", "dbo.StudentType", "Id");
CreateIndex("dbo.Person", "StudentType_Id");
}
It should not be adding the StudentType_Id column or index.
We can be explicit by adding the 'StudentMap' class:
public class StudentMap : EntityTypeConfiguration<Student> {
public StudentMap() {
this.HasOptional(x => x.StudentType)
.WithMany()
.HasForeignKey(x => x.StudentTypeId);
}
}
But no joy..
Indeed, if we delete the database and all the migrations.
Then run add-migration Initial against our new model we get:
public override void Up()
{
CreateTable(
"dbo.Person",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(maxLength: 4000),
StudentTypeId = c.Int(),
Discriminator = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.StudentType", t => t.StudentTypeId)
.Index(t => t.StudentTypeId);
CreateTable(
"dbo.StudentType",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false, maxLength: 100),
})
.PrimaryKey(t => t.Id);
}
In this "correct" version we see that EF Code First migrations uses the StudentTypeId column as expected.
Question
Given that the database already exists, is there a way to tell EF Code First migrations to use the existing StudentTypeId column.
The GitHub repo that demonstrates the problem is here:
https://github.com/paulyk/ef_code_first_proof_of_tph_bug.git
Git tags
1_add_migration_Initial
2_add_migration_person_TPH
3_add_studentMap
There are 3 conventions that I found that relate to the discovery of explicit foreign keys in the class:
System.Data.Entity.ModelConfiguration.Conventions.NavigationPropertyNameForeignKeyDiscoveryConvention
System.Data.Entity.ModelConfiguration.Conventions.PrimaryKeyNameForeignKeyDiscoveryConvention
System.Data.Entity.ModelConfiguration.Conventions.TypeNameForeignKeyDiscoveryConvention
The PrimaryKeyNameForeignKeyDiscoveryConvention would not help here since the primary key on StudentType is just Id. The other two would both match on StudentTypeId though, so as long as you aren't removing both of those, the conventions should pick it up.
According to this question (Foreign key navigation property naming convention alternatives) though, you can also add [ForeignKey("StudentTypeId")] to the StudentType property on Student and [InverseProperty("StudentType")] to the Students property on StudentType.
Hope that helps. :)
Related
I'm trying to create a one-to-many relationship between two models, File and AMCN. Here's my File:
public class File
{
public int Id { get; set; }
public int AmcnId { get; set; }
[StringLength(255)]
public string FileName { get; set; }
[StringLength(100)]
public string ContentType { get; set; }
public byte[] Content { get; set; }
public virtual AMCN Amcn { get; set; }
}
Here's part of my AMCN:
public class AMCN
{
public int Id { get; set; }
public DateTime Created { get; set; }
//lots of other data here...
public virtual ICollection<File> Files { get; set; }
public virtual ICollection<FoSL> FoSLs { get; set; }
}
so I went into the package manager console, did add-migration files, which worked and generated this:
public partial class files : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Files",
c => new
{
Id = c.Int(nullable: false, identity: true),
AmcnId = c.Int(nullable: false),
FileName = c.String(maxLength: 255),
ContentType = c.String(maxLength: 100),
Content = c.Binary(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AMCNs", t => t.AmcnId, cascadeDelete: true)
.Index(t => t.AmcnId);
}
public override void Down()
{
DropForeignKey("dbo.Files", "AmcnId", "dbo.AMCNs");
DropIndex("dbo.Files", new[] { "AmcnId" });
DropTable("dbo.Files");
}
}
but when I do update-database I get an error:
Error Number:1776,State:0,Class:16
There are no primary or candidate keys in the referenced table 'dbo.AMCNs' that match the referencing column list in the foreign key 'FK_dbo.Files_dbo.AMCNs_AmcnId'.
Could not create constraint. See previous errors.
As far as I know, entity framework should be able to figure out that I want AmcnId to be a foreign key pointing to AMCN.Id. In fact, when I made another class, FoSL, it worked fine.
public class FoSL
{
public int Id { get; set; }
public int AmcnId { get; set; }
public string FO { get; set; }
public DateTime StartDate { get; set; }
public string Location { get; set; }
}
generated migration class
public partial class FoSL : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.FoSLs",
c => new
{
Id = c.Int(nullable: false, identity: true),
AmcnId = c.Int(nullable: false),
FO = c.String(),
StartDate = c.DateTime(nullable: false),
Location = c.String(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AMCNs", t => t.AmcnId, cascadeDelete: true)
.Index(t => t.AmcnId);
}
public override void Down()
{
DropForeignKey("dbo.FoSLs", "AmcnId", "dbo.AMCNs");
DropIndex("dbo.FoSLs", new[] { "AmcnId" });
DropTable("dbo.FoSLs");
}
}
I can't see any real difference between the FoSL and File classes, or their migrations. As far as I know, this should work. So what am I doing wrong?
I've tried adding [Key] on AMCN.Id, didn't help.
edit: the initial dbmigration included this for Up():
CreateTable(
"dbo.AMCNs",
c => new
{
Id = c.Int(nullable: false, identity: true),
Created = c.DateTime(nullable: false),
//lots of other data here....
})
.PrimaryKey(t => t.Id);
Turns out the culprit was something I'd forgotten about; I made a backup database for testing so I wouldn't affect the production database until I'm ready. However, when I backed up the data into a new database, it didn't take the keys with it. So my AMCN table in the database didn't have the primary key that entity framework expected it to have. I added a primary key, ran update-database in the package manager console again, and it worked fine.
(not sure if I should delete this question...)
The exact same thing that happened to Adam happened to me. I had created a backup database for testing changes without affecting my production environment. Using the SQL Import/Export Wizard on MS SSMS to make a copy, meant that the PK information for the tables hadn't been copied across, hence the Migration working, but trying to update the DB did not, as no explicit PK could be found.
Manually configured the PK in SSMS Table Designer and the Migration worked perfectly. Posting in case someone makes the same mistake I did!
I am battling with EF code first and trying to map a 1:1 relationship with no joy.
Basically a user can have a userdetail.
I am setting primary keys on both tables.On my UserDetail table has field UserId I am trying to use as the FK.
public class User:BaseModel
{
public virtual UserDetail UserDetail { get; set; }
public string UserName { get; set; }
}
public class UserDetail:BaseModel
{
public virtual User User { get; set; }
[ForeignKey("User")]
public int UserId { get; set; }
public string UserDetailName { get; set; }
}
public class BaseModel{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
}
When I try using the add-migration command I get this error
UserDetail_User_Source: : Multiplicity is not valid in Role 'UserDetail_User_Source' in relationship 'UserDetail_User'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
What do I need to do to get this working? Surely this shouldnt be that difficult?
Update below based on comment from #steve-green
I configured the fluentapi like steve suggested
modelBuilder.Entity<User>()
.HasRequired(t => t.UserDetail)
.WithRequiredPrincipal(t => t.User);
however the generated migration step looks wrong to me
CreateTable(
"dbo.Users",
c => new
{
UserId = c.Int(nullable: false, identity: true),
UserName = c.String(),
})
.PrimaryKey(t => t.UserId);
CreateTable(
"dbo.UserDetails",
c => new
{
UserDetailId = c.Int(nullable: false, identity: true),
UserId = c.Int(nullable: false),
UserDetailName = c.String(),
})
.PrimaryKey(t => t.UserDetailId)
.ForeignKey("dbo.Users", t => t.UserDetailId)
.Index(t => t.UserDetailId);
The FK is configured as UserDetailId shouldnt it be UserId?
Slightly modified model
public class UserDetail
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserDetailId { get; set; }
public virtual User User { get; set; }
public int UserId { get; set; }
public string UserDetailName { get; set; }
}
Get rid of the attribute
[ForeignKey("User")
in UserDetail. Also, i would add
int UserDetailId
to User
That should then work.
If you are going to use a base table, then you can't do it with annotations because (as you mention) the key on the required dependent needs to be a foreign key. You will need fluent api code:
modelBuilder.Entity<User>()
.HasRequired(t => t.UserDetail)
.WithRequiredPrincipal(t => t.User);
https://msdn.microsoft.com/en-us/data/jj591620.aspx#RequiredToRequired
I had entities such as this:
public class Foo
{
public int Id { get; set; }
public Bar Bar { get; set; }
public Bar2 Bar2 { get; set; }
}
public class Bar
{
public int Id { get; set; }
public string Description { get; set; }
}
public class Bar2
{
public int Id { get; set; }
public string Description { get; set; }
}
which migrations are:
CreateTable(
"dbo.Bar",
c => new
{
Id = c.Int(nullable: false, identity: true),
Description = c.String(),
})
.PrimaryKey(t => t.Id);
CreateTable(
"dbo.Bar2",
c => new
{
Id = c.Int(nullable: false, identity: true),
Description = c.String(),
})
.PrimaryKey(t => t.Id);
CreateTable(
"dbo.Foo",
c => new
{
Id = c.Int(nullable: false, identity: true),
Bar_Id = c.Int(),
Bar2_Id = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Bar", t => t.Bar_Id)
.ForeignKey("dbo.Bar2", t => t.Bar_Id)
.Index(t => t.AlertCause_Id)
then I set a Bar property at Foo as virtual and it breaks with "AutomaticMigrationsDisabledException: Unable to update database to match the current model because there are pending changes and automatic migration is disabled." And after the migration re-scaffolding the code changes only in that Bar_Id becomes BarId but for Bar2 it remains Bar2_Id. So I wonder why it gets me the migration re-scaffolded if it seems it does not change anything? Yes it needs the proxy classes and lazy load, etc. but why a new migration? Thanks!
UPDATE
I've missed that the migration was actually triggered by the adding of a foreign key property, BarId. So my mistake here.
Found the reason. This is an incorrect question as the migration was triggered because of the adding a reference property (BarID). My mistake. So closing it.
My entity has a List<SecondEntityDTO>. When EF generates the table, in the table SecondEntities there's a column name FirstEntityDTO_id. I would like this column to be named "ParentEntity_id". How can I do that?
I tried annotating the List of SecondEntityDTO and a bunch of other things...
Edit1: I belive you guys missunderstood.
This is my MainEntity:
[Table("MainEntities")]
public class MainEntityDTO
{
public string Title { get; set; }
[Key]
public int id { get; set; }
public List<SubEntityDTO> SubEntities { get; set; }
}
This is SubEntityDTO:
[Table("SubEntities")]
public class SubEntityDTO
{
[Key]
public int id { get; set; }
public string Title { get; set; }
}
And this is the Migration:
public override void Up()
{
CreateTable(
"dbo.MainEntities",
c => new
{
id = c.Int(nullable: false, identity: true),
Title = c.String(),
Discriminator = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => t.id);
CreateTable(
"dbo.SubEntities",
c => new
{
id = c.Int(nullable: false, identity: true),
Title = c.String(),
MainEntityDTO_id = c.Int(),
})
.PrimaryKey(t => t.id)
.ForeignKey("dbo.MainEntities", t => t.MainEntityDTO_id)
.Index(t => t.MainEntityDTO_id);
}
Note the name of the third column on the SubEntities table!
Also, you can do the same using Fluent Api, for example, overwritting the OnModelCreating method of your Context and doing this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<SecondEntity>().Property(s => s.FirstEntityDTO_id).HasColumnName("ParentEntity_id");
}
Update
Why you don't edit Func<> that specifies the columns of that table. As you can see you are creating an anonymous type, so you can change the name of the column there, eg:
CreateTable(
"dbo.SubEntities",
c => new
{
id = c.Int(nullable: false, identity: true),
Title = c.String(),
ParentEntity_id = c.Int(),
})
.PrimaryKey(t => t.id)
.ForeignKey("dbo.MainEntities", t => t.ParentEntity_id)
.Index(t => t.ParentEntity_id);
If you do this, remember change the name of that property in the Down method, but if you already executed that script, don't change the FK name yet in the Down method. Execute again the Update Database command specifying the name of that script. That will drop those tables and they will be created once again using the Up method, but now with the FK name that you want it.In that moment is when you can change the FK name in the Down method:
public override void Down()
{
DropForeignKey("dbo.SubEntities", "ParentEntity_id", "dbo.MainEntities");
DropIndex("dbo.SubEntities", new[] { "ParentEntity_id" });
DropTable("dbo.SubEntities");
DropTable("dbo.MainEntities");
}
Put the following code in your SubEntity class:
[ForeignKey("ParentEntity")]
public int ParentEntity_id { get; set; }
public virtual MainEntity ParentEntity { get; set; }
I have a simple model using table per type inheritance for some entities. The problem is that when I generate the migration using Add-Migration, It creates a duplicated index on the child class' primary key.
Class definitions:
class Product
{
[Key]
public int ProductId { get; set; }
public int Value { get; set; }
}
class Service : Product
{
public int OtherValue { get; set; }
}
And in my context, I specify the table names for both classes
class ProductContext : DbContext
{
virtual public DbSet<Product> ProductSet { get; set; }
virtual public DbSet<Service> ServiceSet { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().ToTable("Product");
modelBuilder.Entity<Service>().ToTable("Service");
}
}
Running Add-Migration results in the following:
public override void Up()
{
CreateTable(
"dbo.Product",
c => new
{
ProductId = c.Int(nullable: false, identity: true),
Value = c.Int(nullable: false),
})
.PrimaryKey(t => t.ProductId);
CreateTable(
"dbo.Service",
c => new
{
ProductId = c.Int(nullable: false),
OtherValue = c.Int(nullable: false),
})
.PrimaryKey(t => t.ProductId)
.ForeignKey("dbo.Product", t => t.ProductId)
.Index(t => t.ProductId);
}
It creates an additional index on Service.ProductId when it's already the primary key. Is there some annotation I am missing in order to prevent the index from being added?
Tested with both EF5 and EF6 with the same results.
Just for whom (still) facing this problem, as I understand it's a bug fixed in version 6.1.1 of EF (https://entityframework.codeplex.com/workitem/1035).
So just updating to the latest version of EF should fix it. But if you couldn't or wouldn't update, the workaround is just as simple as deleting duplicate Index in generated migration file and save (don't also forget to disable AutomaticMigrationsEnabled if enabled).
I suspect that the Entity Framework adds an index to foreign keys - even when the foreign key is already indexed because it is also the primary key. Maybe it's an oversight or maybe it's a low priority for the framework developers. Either way, you can adjust the Up() method yourself. See item 7 on this useful blog Tips for Entity Framework Migrations
Try making both tables inherit from an abstract class as described here
public abstract class ProductBase
{
[Key]
public int ProductId { get; set; }
}
public class Product: ProductBase
{
public int Value { get; set; }
}
public class Service : ProductBase
{
public int OtherValue { get; set; }
}