EF 简单插入
这是最奇怪的事情...也许我有一个金发时刻或其他什么但是为什么这不起作用...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原来我是个傻瓜,忘记在数据库中的 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 :)