EF 简单插入

发布于 2024-11-30 17:18:54 字数 772 浏览 0 评论 0原文

这是最奇怪的事情...也许我有一个金发时刻或其他什么但是为什么这不起作用...

FLMCommsEntities dal = new FLMCommsEntities();
foreach (Email email in mail)
{
    dal.EmailReceiveds.AddObject(new EmailReceived
    {
        FromAddress = email.From,
        ToAddress = email.To,
        EmailSubject = email.Subject,
        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();

它返回一个sql异常说我无法插入重复的主键值...显然我'我根本不想插入主键值,而是创建一个新的值!

咕噜咕噜...

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,
        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 ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画中仙 2024-12-07 17:18:54

原来我是个傻瓜,忘记在数据库中的 PK 字段上设置“身份规范”,因此 EF 不知道它是用 SQL Server 生成的 ID 自动填充的。

没关系...

最终到达那里:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文