开发者

EF Simple insert

开发者 https://www.devze.com 2023-03-28 19:05 出处:网络
This is the strangest thing ... maybe i\'m having a blonde moment or something but why doesn\'t this work ...

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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消