You can tell from the following code that things aren't going quite as planned:
DateTime now = DateTime.Now;
fieldBooking.CreatedAt = now;
fieldBooking.CreatedBy = user;
fieldBooking.UpdatedAt = now;
fieldBooking.Field = field;
fieldBooking.FieldBookingDateRange.CreatedAt = now;
fieldBooking.FieldBookingDateRange.CreatedBy = user;
fieldBooking.FieldBookingDateRange.UpdatedAt = now;
fieldBooking.FieldBookingDateRange.FieldBooking = fieldBooking;
fieldBooking.FieldBookingDateRange.DateRange.CreatedAt = now;
fieldBooking.FieldBookingDateRange.DateRange.CreatedBy = user;
fieldBooking.FieldBookingDateRange.DateRange.UpdatedAt = now;
FieldBookingMessageThread fieldBookingMessageThread = new FieldBookingMessageThread()
{
CreatedAt = now,
CreatedBy = user,
UpdatedAt = now,
SentTo = field.CreatedBy,
FieldBooking = fieldBooking,
Subject = user.Profile.FirstName + " has made a booking for your field: " + field.Name,
FieldBookingMessages = new List<FieldBookingMessage>()
};
fieldBookingMessageThread.FieldBookingMessages.Add(
new FieldBookingMessage(){
FieldBookingMessageThread = fieldBookingMessageThread,
Body = Request.Params["FieldBookingComment"],
CreatedAt = now,
CreatedBy = user,
UpdatedAt = now,
SentTo = field.CreatedBy
}
);
fieldBooking.FieldBookingMessageThread = new List<FieldBookingMessageThread>()
{
fieldBookingMessageThread
};
this.unitOfWork.FieldBookingRepository.Insert(fieldBooking);
It's a mess.
- I'm having to set CreatedAt and CreatedBy etc on all of my entities regardless of all of them having that...
- I'm having to relate each object to each other
- The application doesn't even like it:
Cannot insert the value NULL into column 'FieldBookingMessageThreadID', table '**.dbo.FieldBookingMessageThreads'; column does not allow nulls. INSERT fails. The statement has been terminated.
The problem seems to be that the entities must first be created, in order to link them all up... But this can't be right... there must be a way of making entity framework do the bulk of this work.
What am I missing out, where am I going wrong?
Extra info:
Field booking
public class FieldBooking : ISoftDeletable, ITimeStamps, ICreatedBy
{
[Key]
public int FieldBookingID { get; set; }
[Display( Name = "Created at" )]
public DateTime CreatedAt { get; set; }
[Display( Name = "Updated at" )]
public DateTime UpdatedAt { get; set; }
...
public virtual List<FieldBookingMessageThread> FieldBookingMessageThread { get; set; }
}
Field booking message thread
public class FieldBookingMessageThread : MessageThread, ISoftDeletable, ITimeStamps, ICreatedBy
{
[Key]
public int FieldBookingMessageThreadID { get; set; }
[Display( Name = "Field" )]
[Required]
public virtual FieldBooking FieldBooking { get; set; }
...
}
MessageThread inherited by FieldMessageThread:
public class MessageThread: ISoftDeletable, ITimeStamps, ICreatedBy
{
[Display(Name = "Created at")]
public DateTime CreatedAt { get; set; }
[Display(Name = "Updated at")]
public DateTime UpdatedAt { get; set; }
[Display(Name = "Deleted at")]
public DateTime? DeletedAt { get; set; }
[Display(Name = "Created by")]
public virtual ApplicationUser CreatedBy { get; set; }
[Display(Name = "Sent to")]
public virtual ApplicationUser SentTo { get; set; }
[Required]
[MaxLength(350)]
[Display(Name = "Subject")]
public string Subject { get; set; }
}
Field Message class
public class FieldMessage : Message, ISoftDeletable, ITimeStamps, ICreatedBy
{
[Key]
public int FieldMessageID { get; set; }
[Required]
public virtual FieldMessageThread FieldMessageThread { get; set; }
}
Error output of insert:
INSERT [dbo].[FieldBookingMessageThreads]([CreatedAt], [UpdatedAt], [DeletedAt], [Subject], [CreatedBy_Id], [FieldBooking_FieldBookingID], [SentTo_Id])
VALUES (@0, @1, NULL, @2, @3, @4, @5)
SELECT [FieldBookingMessageThreadID]
FROM [dbo].[FieldBookingMessageThreads]
WHERE @@ROWCOUNT > 0 AND [FieldBookingMessageThreadID] = scope_identity()
-- @0: '28/04/2015 15:20:46' (Type = DateTime2)
-- @1: '28/04/2015 15:20:46' (Type = DateTime2)
-- @2: 'Super has made a booking for your field: Test' (Type = String, Size = 350)
-- @3: 'fb2f22ca-8de9-4206-99ce-d43af3d776da' (Type = String, Size = 128)
-- @4: '6' (Type = Int32)
-- @5: 'fb2f22ca-8de9-4206-99ce-d43af3d776da' (Type = String, Size = 128)
-- Executing at 28/04/2015 15:20:56 +01:00
-- Failed in 76 ms with error: Cannot insert the value NULL into column 'FieldBookingMessageThreadID', table 'FieldLover.dbo.FieldBookingMessageThreads'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Closed connection at 28/04/2015 15:20:56 +01:00
A first chance exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll
'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2063751123/ROOT-5-130747043423504979): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'.