I have some complex JSON I am loading into directly into a model class but I want to ensure that if the root record is deleted the child records will be deleted too.
However, due to the fact I'm using a ComplexType I can't seem to get the syntax right.
The rest of the very large JSON piece is working, but the part represented by the simplified example below is not.
Sample Event JSON:
{"eventId":12345,
"zone": {
"beach": {
"score": 19,
"team": [{
"id": "abcdef",
"name": "Barry",
"playerType": 2
},
{
"id": "bvfe43",
"name": "Adam",
"playerType": 1
},
{
"id": "g3yh6",
"name": "Mary",
"playerType": 1
}]
},
"cave": {
"score": 1,
"team": [{
"id": "qPoavMkCTeKIy_htsvSKpQ",
"name": "CAPITALMONCALAMARICRUISER",
"playerType": 3
},
{
"id": "ggsd84",
"name": "Tibor",
"playerType": 1
},
{
"id": "2332edc",
"name": "Jun",
"playerType": 1
},
{
"id": "f234fg54",
"name": "Aaron",
"playerType": 1
}]
}
}
Every Event has a zone.beach and a zone.cave, and both zone.beach and zone.cave had a score and a team.
My ef model is:
public class Event
{
[Key]
public int id { get; set; }
public int eventId { get; set;}
public Zone zone { get; set;}
}
[ComplexType]
public class Zone
{
public Beach beach { get; set; }
public Cave cave { get; set; }
}
[ComplexType]
public class Beach
{
public int score { get; set; }
public ICollection<Team> team { get; set; }
}
[ComplexType]
public class Cave
{
public int score { get; set; }
public ICollection<Team> team { get; set; }
}
public class Team
{
[Key]
public int teamId { get; set;}
public string id { get; set;}
public string name { get; set;}
public int playerType { get; set;}
}
And I load the json like this:
var event = JsonConvert.DeserializeObject<Event>(content);
But when by database initializes with this code:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Event>()
.HasMany(a => a.zone.beach.team)
.WithRequired()
.WillCascadeOnDelete(true);
modelBuilder.Entity<Event>()
.HasMany(a => a.zone.cave.team)
.WithRequired()
.WillCascadeOnDelete(true);
}
I get an exception:
System.InvalidOperationException
HResult=0x80131509
Message=The expression 'a => a.zone.beach.team' is not a valid property expression. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Utilities.ExpressionExtensions.GetSimplePropertyAccess(LambdaExpression propertyAccessExpression)
at System.Data.Entity.ModelConfiguration.EntityTypeConfiguration1.HasMany[TTargetEntity](Expression
1 navigationPropertyExpression)
User contributions licensed under CC BY-SA 3.0