Jonas
Jonas

Reputation: 4584

Complex types in EF 6.2

I have a setup like this (existing, stored in a document database. We're investigating migration to SQL using EF):

public class MyClassInTheDbSet : BaseClass
{
}

public abstract class BaseClass
{
  public Staff LastModifiedBy {get;set;}
  public Stamp LastModifiedStamp {get;set;}
}

[ComplexType]
public class Staff
{
  public string Name {get;set:}
  public string Id {get;set;}
}

[ComplexType]
public class Stamp
{
  public Staff ModifiedBy {get;set;}
  public Staff CreatedBy {get;set;} //these 2 were being set to the same thing
  public DateTimeOffset? ModifiedOn {get;set;}
}

When trying to save this entity into a database, I get a message saying that "MyClassInTheDbSet references the same complex of type "Staff" more than once." The entity persists without problem, despite the error message. Is this just an unavoidable error in EF? Is there some way to work around this without completely changing our existing class design?

Upvotes: 1

Views: 114

Answers (2)

Jonas
Jonas

Reputation: 4584

The lesson, as always, I'm an idiot.

I had the same Complex Type (staff) referenced twice in the Stamp object (which I neglected to show originally); turns out they were both being set to the same object.

Upvotes: 0

jbl
jbl

Reputation: 15413

I guess the issue happens when CreatedBy and ModifiedBy are the same Staff instance. As far as I know, it should not work. Maybe the fact that you are using nested complex types helps here.

Is there some way to work around this without completely changing our existing class design ?

I think simply cloning the Staff instance so that CreatedBy and ModifiedBy reference different instances should solve the issue.

Upvotes: 1

Related Questions