Okay, this is getting ridiculous as this is turning out to be much more difficult than it has any right to be.
If I use my original code with no FluentAPI mapping, I have a ParentID field which is not used, and a new field called Node_ID is used.
public class Node {
public long ID { get; private set; }
public long ParentID { get; set; }
public ICollection<Node> Children { get; set; }
}
Here are my various attempts:
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Entity<Node>()
.HasMany<Node>(h => h.Children)
.WithOptional()
.HasForeignKey(h => h.ParentID);
}
DbUpdateException: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Entity<Node>()
.HasMany<Node>(h => h.Children)
.WithOptional()
.Map(m => m.MapKey("ParentID"));
}
MetadataException: Schema specified is not valid. Errors:
(82,6) : error 0019: Each property name in a type must be unique. Property name 'ParentID' was already defined.
[ForeignKey("ParentID")]
public ICollection<Node> Children { get; set; }
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Entity<Node>()
.HasMany<Node>(h => h.Children)
.WithOptional()
}
DbUpdateException: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
Update
Using the Fluent API code from my first attempt code above (.HasForeignKey), and by making ParentID nullable (public long? ParentID), I have gotten the database to successfully map. Is there any way to do this without making the FK nullable? I would like the key to be 0 when no parent exists. If not, oh well, I will deal.
No there is no way to avoid nullable ParentId - you told EF that parent is optional (it must be otherwise you will not be able to use the table) and because of that related FK property must be nullable.
Related
In previous versions of EF I can use following code to implement an identifying relationship:
public class Child
{
[Key, Column(Order = 1)]
public virtual int Id { get; set; }
[Key, Column(Order = 2)]
public virtual int ParentId { get; set; }
public virtual Parent Parent { get; set; }
}
It's needed to easily remove a child from collection like this:
var parent = _context.Parents.First();
var child = parent.Children.First();
parent.Children.Remove(child);
_context.SaveChanges();
This approach is described in http://www.kianryan.co.uk/2013/03/orphaned-child/ (the method #2).
But in EF7 this code throws exception when migration is creating:
An exception was thrown while executing a resolve operation. See the
InnerException for details. ---> Entity type 'Child' has composite
primary key defined with data annotations. To set composite primary
key, use fluent API.
I also tried to use FluentAPI as described in How to define nested Identifying Relationships Entity Framework code first in following code:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Parent>()
.HasMany(p => p.Children)
.WithOne(c => c.Parent);
modelBuilder.Entity<Child>()
.HasKey(c => new {c.Id, c.ParentId});
base.OnModelCreating(modelBuilder);
}
This approach allow generate a migration successfully, but when I tried to remove a child from Children collection I got following exception:
System.InvalidOperationException: The association between entity types
'Parent' and 'Child' has been severed but the foreign key for this
relationship cannot be set to null. If the dependent entity should be
deleted, then setup the relationship to use cascade deletes.
But I wouldn't like to use cascade deletes, I would like to use identifying relationship!
Please, help me understand what I do incorrect. Thank you!
Use cascade on delete instead as this is what it's used for:
modelBuilder.Entity<Parent>()
.HasMany(p => p.Children)
.WithOne(c => c.Parent);
.WillCascadeOnDelete(true);
https://msdn.microsoft.com/en-gb/data/jj591620.aspx
Just in case someone sees this error, let me tell you how I resolved mine:
When you do an update, on EF you need to first query the database and get the data model, then map the Domain layer model with your changes onto it (basically copying fields onto the data), and finally call the DBContext update method, then save changes.
My problem was that my model (not the data model, the domain model) also had the sub objects on it.
So here's the data layer model (for example):
public class Parent
{
public int ChildId {get; set; }
[ForeignKey("ChildId")]
public virtual Child Child { get; set; }
}
And here's how the domain layer model should be:
public class Parent
{
public int ChildId { get; set; }
//public Child Child { get; set; } // this caused the error, keep reading if you want to know more.
}
When I was seeing the error, I had been using Autofac's runtime mapper to map the domain layer model's properties onto the data layer model. However, the child in the domain layer model was null, so it would nullify the data layer, causing the error:
"The association between entity types 'Parent' and 'Child' has been severed but the foreign key for this relationship cannot be set to null. If the dependent entity should be deleted, then setup the relationship to use cascade deletes."
By the way, in the db context class, I have the following relationship defined:
modelBuilder.Entity<Parent>()
.HasOne(a => a.Child)
.WithMany()
.HasForeignKey(p => p.ChildId)
.OnDelete(DeleteBehavior.Restrict);
It's working.
We use Entity Framework 6 with CodeFirst and an Oracle.ManagedDataAccess.
I just created a 1 to 0..1 (one to zero or one) relation between two tables, and it works like a charm. But when adding a second relation, I got into trouble, because the original primary key column was demoted to just a data column, and a surrogate (sequence) primary key column was added. The foreign key constraint is still on the old field.
Code:
public class Node
{
[Key, Column("ID"), Required]
public int Id { get; set; }
[Column("POINT_CODE"), Required, StringLength(10)]
public string PointCode { get; set; }
// ...columns left out...
[ForeignKey("PointCode"), Required]
public NetworkPoint PointCodeFk { get; set; }
}
public class Point
{
[Key, Column("POINT_CODE"), Required, StringLength(10)]
public string PointCode { get; set; }
// ...columns left out...
[ForeignKey("PointCode")]
public Node NodeFk { get; set; }
}
public class MyDbContext : EntityContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Point>()
.HasOptional(m => m.NodeFk)
.WithRequired(o => o.PointCodeFk);
}
// ...stuff left out...
}
The difference with the working example is, the property Node.PointCode has the attribute Key, and there is no Node.Id column.
When running this example, I get the error message:
Multiplicity is not valid in Role 'Point_NodeFk_Target' in relationship 'Point_NodeFk'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
So I guess EntityFramework now thinks it should be an 1:N relation.
How can this be solved? I can't change the database (although I would like to very much).
Entity Framework requires a primary key on each table. You need to add a primary without which EF will complain. So looking at your code Point class require ID, Id ,PointId or PointID property so EF will know it has a primary key. Any of the conventions I showed here as a class property will be acceptable to EF.
I have this model:
public class ContentType
{
public int ContentTypeId{get;set;}
public string Name{get;set;}
public Lang Lang{get;set;}
public bool IsPublished{get;set;}
public int? ParentId { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
public virtual ContentType Parent { get; set; }
public virtual List<ContentType> Children { get; set; }
}
It has a one to many relation to itself.
And in Context I have this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ContentType>().HasMany(c => c.Children).WithOptional(c => c.Parent).HasForeignKey(c => c.ParentId);
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
But when I am saving a record with ParentId=0 I see this error:
Unable to determine a valid ordering for dependent operations.
Dependencies may exist due to foreign key constraints, model
requirements, or store-generated values.
Note that the relation in database is not exist because of the following error:
'ContentType' table
- Unable to create relationship 'FK_ContentType_ContentType'. The ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE
constraint "FK_ContentType_ContentType". The conflict occurred in
database "CMS", table "dbo.ContentType", column 'ContentTypeId'.
But I don't think the problem be from here.I don't know.
What is it wrong here?
It seems that both errors are caused by the incorrect data in the table ContentType.
'ContentType' table - Unable to create relationship
'FK_ContentType_ContentType'. The ALTER TABLE statement conflicted
with the FOREIGN KEY SAME TABLE constraint
"FK_ContentType_ContentType". The conflict occurred in database "CMS",
table "dbo.ContentType", column 'ContentTypeId'.
This points to the incorrect value in ParentId field. Verify that values in this field are in fact correct ContentTypeId in other records.
Unable to determine a valid ordering for dependent operations.
Dependencies may exist due to foreign key constraints, model
requirements, or store-generated values.
This error points to a circular dependency. Verify that you do not have cycles in your data.
I am using Entity Framework code first.
I was trying to put 2 foreign keys on the same table but I am not being allowed to do so.
It's like that(edited) :
The table is called Avaliacao:
[Required]
public int AvaliacaoId { get; set; }
[Required]
public int LivroId { get; set; }
public int? AutorId { get; set; }
public virtual Livro Livro { get; set; }
public virtual Autor Autor { get; set; }
I wanted AutorId not to be null but it only works that way.
I wish I could have 2 non-nullable FK but only one Delete on Cascade.
How do I achieve this with Entity Framework code-first?
Somebody help me please
thx in advance
ZeCarioca
EDIT:
I have not tested this, but if you are using EF5 you could make use of the OnModelCreating method by overiding it in your DbContext. You can call the same entity multiple times to add configuration so you could specify a second foreign key, set its HasRequired property and set its WillCascadeOnDelete property to true.
Something like this for the first foreign key.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Avaliacao>()
.HasRequired(a => a.LivroId)
.HasForeignKey(m => a.LivroId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Avaliacao>()
.HasRequired(a => a.AutorId)
.HasForeignKey(m => a.AutorId)
.WillCascadeOnDelete(false);
}
For more reference on the method you can look here at the MSDN Docs: DbModelBuilder
As mentioned I have not tested this myself so you might need to change some of the properties.
Hope it helps
You can do this through the fluent API, for example:
modelBuilder.Entity<MyEntity>().HasRequired(t => t.Livro);
modelBuilder.Entity<MyEntity>().HasRequired(t => t.Autor);
Error:
Unhandled Exception: System.Data.SqlClient.SqlException: The operation failed because an index or statistics with name 'IX_ID' already exists on table 'PrivateMakeUpLessons'.
Model (Simplified, building in a separate test project for debugging):
public abstract class Lesson
{
public Guid ID { get; set; }
public string Room { get; set; }
public TimeSpan Time { get; set; }
public int Duration { get; set; }
}
public abstract class RecurringLesson : Lesson
{
public int DayOfWeek { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string Frequency { get; set; }
}
public class PrivateLesson : RecurringLesson
{
public string Student { get; set; }
public string Teacher { get; set; }
public virtual ICollection<Cancellation> Cancellations { get; set; }
}
public class Cancellation
{
public Guid ID { get; set; }
public DateTime Date { get; set; }
public virtual PrivateLesson Lesson { get; set; }
public virtual MakeUpLesson MakeUpLesson { get; set; }
}
public class MakeUpLesson : Lesson
{
public DateTime Date { get; set; }
public string Teacher { get; set; }
public virtual Cancellation Cancellation { get; set; }
}
Configuration:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Lesson>().ToTable("Lessons");
modelBuilder.Entity<RecurringLesson>().ToTable("RecurringLessons");
modelBuilder.Entity<PrivateLesson>().ToTable("PrivateLessons");
modelBuilder.Entity<MakeUpLesson>().ToTable("PrivateMakeUpLessons");
modelBuilder.Entity<Cancellation>()
.HasOptional(x => x.MakeUpLesson)
.WithRequired(x => x.Cancellation);
base.OnModelCreating(modelBuilder);
}
Notes:
This worked fine in EF 4.2. Is there something wrong with my model? The actual model is much more complicated which is why I have all the classes abstracted out. Also, I am working against an existing database so I need to use Table-Per-Type inheritance.
If I change the relationship of Cancellation to PrivateMakeUpLesson from 1 to 0..1 to 0..1 to 0..1 it works. This is undesirable because you can't have a PrivateMakeUpLesson without a Cancellation.
Also, if I make PrivateMakeUpLesson NOT inherit from Lesson then it also works, but it IS a lesson and needs to remain so for existing business logic.
I'd appreciate any guidance. Thank you!
Edit:
Starting a bounty. I can't find any documentation on what changed between EF 4.2 and EF 4.3 with regard to the index generation for code first. It's clear that EF 4.3 is creating more indexes and that the naming scheme has changed but I want to know if there's a bug in EF or if there is something fundamentally wrong with my model or fluent API configuration.
As of EF 4.3, indexes are added for freign key columns during database creation. There is a bug that can cause an index to be created more than once. This will be fixed in a future EF release.
Until then, you can work around the issue by creating your database using Migrations instead of database initializers (or the Database.Create() method).
After generating the initial migration, you will need to delete the redundant call to Index().
CreateTable(
"dbo.PrivateMakeUpLessons",
c => new
{
ID = c.Guid(nullable: false),
...
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Lessons", t => t.ID)
.ForeignKey("dbo.Cancellations", t => t.ID)
.Index(t => t.ID)
.Index(t => t.ID); // <-- Remove this
To continue creating your database at run-time, you can use the MigrateDatabaseToLatestVersion initializer.
In my opinion this is clearly a bug.
The problem starts with the observation that EF creates an index IX_ID at all. If you strip down the model to the following...
public abstract class Lesson
{
public Guid ID { get; set; }
}
public class RecurringLesson : Lesson
{
}
public class MyContext : DbContext
{
public DbSet<Lesson> Lessons { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<RecurringLesson>().ToTable("RecurringLessons");
}
}
... and let EF create the database schema you get two tables Lessons and RecurringLessons as expected for a TPT inheritance mapping. But I am wondering why it creates two indices for the table RecurringLessons:
Index PK_RecurringLessons (clustered, unique) with Index column ID
Index IX_ID (not clustered, not unique) with Index column ID again
I don't know if there is any benefit for the database to have a second index on the same column. But for my understanding it doesn't make sense 1) to create an index on the same column that is already covered in the PK clustered index, and 2) to create a not unique index on a column which is the primary key and therefore necessarily unique.
Moreover due to the one-to-one relationship EF tries to create an index on the table of the dependent of this association which is PrivateMakeUpLessons. (It's the dependent (and not the principal) because Cancellation is required in entity MakeUpLesson.)
ID is the foreign key in this association (and primary key at the same time because one-to-one relationships are always shared primary key associations in Entity Framework). EF apparently always creates a index on the foreign key of a relationship. But for one-to-many relationships this is not a problem because the FK column is different from the PK column. Not so for one-to-one relatonships: The FK and PK are the same (that is ID), hence EF tries to create an index IX_ID for this one-to-one relationship which already exists due to the TPT inheritance mapping (which leads to a one-to-one relationship as well from database perspective).
The same consideration as above applies here: The table PrivateMakeUpLessons has a clustered PK index on column ID. Why is a second index IX_ID on the same column required at all?
In addition EF doesn't seem to check that it already wants to create an Index with name IX_ID for the TPT inheritance, leading finally to the exception in the database when the DDL is sent to create the database schema.
EF 4.2 (and before) didn't create any indices (except PK indices) at all, this was introduced in EF 4.3, especially indices for FK columns.
I didn't find a workaround. In the worst case you have to create the database schema manually and avoid that EF tries to create it (= disable database initialization). In the best case there is a way to disable automatic FK index creation, but I don't know if it's possible.
You can submit a bug report here: http://connect.microsoft.com/VisualStudio
Or maybe someone from EF development team will see your question here and provide a solution.
I got a very similar error to this one in my code a while back. Try putting the cancellation list inside the Lesson class. That's what solved my problem.
Below I describe 2 scenarios what is probably going wrong. Please read in depth by clicking the links I provided to know more about my explanation.
First
Lesson and RecurringLesson are abstract classes (so you want to have it as the base classes).
You are creating a table of the Lesson and the RecurringLesson entities which will result in a Table per hierarchy structure.
brief description
Creating a class of the base table will result in one big table which contains the columns of all inherited tables. So all properties of PrivateLesson, MakeUpLesson and all others inherited entities will be stored in the Lessons table. EF will add also a Discriminator column. The value of this column defaults to the persistent class name (like "PrivateLesson" or "MakeUpLesson") only the column matching to that particular entity (matching the Discriminator value) will be used in that particular row.
BUT
You are also mapping the inherited classes like PrivateLesson and MakeUpLesson. This will force EF to use the Table per Type structure which results in one table per class. This can cause conflicts you are facing right now.
Second
Your example shows you have an one-to-one relationship (Cancellation -> MakeUpLesson) and a one-to-many relationship (Cancellation -> PrivateLesson) because PrivateLesson and MakeUpLessonare both (indirect) inherited from Lesson in combination with the first described scenario can cause problems because it will result in 2 foreign key relationships in the database per entity. (one using Table per hierarchy structure and one using the Table per Type structure).
Also this post can help you defining a correct one-to-one definition.
Please verify by performing the following steps:
I assume you have your own test environment so you can create new test databases
1.
Delete the relationships to the Cancellation by commenting out all properties to this class:
public class PrivateLesson : RecurringLesson
{
public string Student { get; set; }
public string Teacher { get; set; }
//public virtual ICollection<Cancellation> Cancellations { get; set; }
}
public class Cancellation
{
public Guid ID { get; set; }
public DateTime Date { get; set; }
//public virtual PrivateLesson Lesson { get; set; }
//public virtual MakeUpLesson MakeUpLesson { get; set; }
}
public class MakeUpLesson : Lesson
{
public DateTime Date { get; set; }
public string Teacher { get; set; }
//public virtual Cancellation Cancellation { get; set; }
}
And remove the configuration to it:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Lesson>().ToTable("Lessons");
modelBuilder.Entity<RecurringLesson>().ToTable("RecurringLessons");
modelBuilder.Entity<PrivateLesson>().ToTable("PrivateLessons");
modelBuilder.Entity<MakeUpLesson>().ToTable("PrivateMakeUpLessons");
//modelBuilder.Entity<Cancellation>()
// .HasOptional(x => x.MakeUpLesson)
// .WithRequired(x => x.Cancellation);
base.OnModelCreating(modelBuilder);
}
2.
Create a new empty database
3.
Let EF generate the table structure for you in this empty database.
4.
Verify the first scenario. If that's true this need to be fixed first by using the Table per hierarchy structure OR the Table per Type structure. Probably you want to use the Table per hierarchy structure because (if I understand your question well) there is already an production environment.
When my project was updated from EF 6.0.2 to EF 6.1.1, I had such a problem, then back to 6.0.2, after the return of an older version, the error disappeared