I have a following piece of code
var newPost = new Post()
{
Activity = new Activity { Type = 1, ActivityTotalStatistic = new ActivityTotalStatistic() },
CreatedDate = oldPost.DateTimeCreated,
CategoryId = categoryId,
Title = oldPost.Name,
OwnerId = oldPost.UserID,
Slug = oldPost.Name,
LastUpdateDate = oldPost.DateTimeCreated,
PublishDate = oldPost.DateTimeCreated,
PostStatistic = new PostStatistic(),
PostItems = new List<PostItem>
{
new PostItem
{
Activity = new Activity { Type = 2},<-- note this line of code
CreatedDate = oldPost.DateTimeCreated,
Title = oldPost.Name,
Type = 1,
Content = oldPost.Path
}
}
};
newDb.Posts.Add(newPost);
newDb.SaveChanges();
This is SQL schema for activity and statistics table
create table ActivityTotalStatistics
(
Id int primary key identity(1,1),
NumberOfLikes int not null,
NumberOfDislikes int not null,
SumOfLikes int not null,
CommentCount int not null
)
create table Activities
(
Id int identity (1,1) primary key,
Type int not null,
ActivityTotalStatisticId int not null
foreign key references ActivityTotalStatistics(Id)
)
As you can see, each activity should have activity statistic, because foreign key is not nullable, and my code should break, because Activity within post item does not have activity statistic instantiated.
But EF does not recognize it. What happens is that post item receives same activity statistic from this line of code
Activity = new Activity { Type = 1, ActivityTotalStatistic = new ActivityTotalStatistic() },
Is this a valid behavior of Entity Framework?
UPDATE Activity.cs model
public partial class Activity
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Activity()
{
this.ActivityLikes = new HashSet<ActivityLike>();
this.Comments = new HashSet<Comment>();
this.PostItems = new HashSet<PostItem>();
this.Posts = new HashSet<Post>();
}
public int Id { get; set; }
public int Type { get; set; }
public int ActivityTotalStatisticId { get; set; }
public virtual ActivityTotalStatistic ActivityTotalStatistic { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ActivityLike> ActivityLikes { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Comment> Comments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PostItem> PostItems { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Post> Posts { get; set; }
}
ActivityTotalStatistic.cs
public partial class ActivityTotalStatistic
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public ActivityTotalStatistic()
{
this.Activities = new HashSet<Activity>();
}
public int Id { get; set; }
public int NumberOfLikes { get; set; }
public int NumberOfDislikes { get; set; }
public int SumOfLikes { get; set; }
public int CommentCount { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Activity> Activities { get; set; }
}