EF Simple insert
This is the strangest thing ... maybe i'm having a blonde moment or something but why doesn't this work ...
FLMCommsEntities dal = new FLMCommsEntities();
foreach (Email email in mail)
{
dal.EmailReceiveds.AddObject(new EmailReceived
{
FromAddress = email.From,
ToAddress = email.To,
EmailSubject = email.Subject,
开发者_StackOverflow EmailBodyHtml = email.BodyHtml,
EmailBodyPlain = email.BodyPlain,
ReceivedOn = (DateTime)email.Date,
AttachmentPath = email.AttachmentPath,
EmailSize = email.Size,
CreatedDate = DateTime.Now,
DownloadComplete = true,
ServerEmailID = email.ServerId
});
}
// ask dal to save the new mail batch locally
dal.SaveChanges();
It returns a sql exception saying i can't insert a duplicate primary key value ... clearly i'm not trying to insert a primary key value at all but make a new one instead !!!
GRRR ...
Turns out i'm a dumbass and forgot to set "Identity Specifiction" on the PK field in the DB so EF didn't know that it was auto populated with a generated ID from the SQL Server.
Never mind ...
Got there in the end :)
精彩评论