我尝试使用此代码来逃避错误
当IDENTITY_INSERT设置为OFF时,无法在表'T_PreviewDetails'中为identity列插入显式值
但同样的错误也发生在这里。任何解决方案?
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[T_PreviewDetails] ON");
context.SaveChanges();
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[T_PreviewDetails] OFF");
这是模型类代码:
namespace AP.Model
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class T_PreviewDetails
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public T_PreviewDetails()
{
T_PreviewDetailsComments = new HashSet<T_PreviewDetailsComments>();
}
//[DatabaseGenerated(DatabaseGeneratedOption.None)]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[StringLength(50)]
public string Title { get; set; }
public int? Preview_ID { get; set; }
public int? SelectionListDetails_ID { get; set; }
public int? PreviewStatus_ID { get; set; }
public virtual M_PreviewStatus M_PreviewStatus { get; set; }
public virtual T_Preview T_Preview { get; set; }
public virtual T_SelectionListDetails T_SelectionListDetails { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<T_PreviewDetailsComments> T_PreviewDetailsComments { get; set; }
}
}
PreviewController.cs包含以下代码:
using(APM context = new APM())
{
T_UserAlbums objAlbum = new T_UserAlbums();
T_UserAlbumDetails objAlbumDetails = new T_UserAlbumDetails();
T_SelectionList objSelectList = new T_SelectionList();
T_SelectionListDetails objSelectListDetails = new T_SelectionListDetails();
T_Preview objprev = new T_Preview();
T_PreviewDetails objprevdet = new T_PreviewDetails();
var lstfile1 = (from s in context.T_OrderDetails
join p in context.T_SelectionList on s.SelectionList_ID equals p.ID
join q in context.T_SelectionListDetails on p.ID equals q.SelectionList_ID
join r in context.T_UserAlbumDetails on q.UserAlbumDetails_ID equals r.ID
where s.IsPreviewRequired == 1 && p.User_ID == id
select new Preview
{
albumid = (int)q.UserAlbumDetails_ID,
selecttionid = (int)q.SelectionList_ID,
p_path = r.PhysicalPath,
v_path = r.VirtualPath,
user = (int)p.User_ID,
orderid = s.ID
}).ToList();
var items = lstfile1.GroupBy(m => m.selecttionid).ToList();
foreach (var itm in items)
{
objAlbum.Title = "preview";
objAlbum.Description = "preview";
objAlbum.User_ID = 32;
objAlbum.CreatedDate = DateTime.Now;
objAlbum.Modifieddate = DateTime.Now;
context.T_UserAlbums.Add(objAlbum);
objSelectList.CreatedDate = DateTime.Now;
objSelectList.ModifiedDate = DateTime.Now;
objSelectList.Title = "Preview";
objSelectList.User_ID = id;
context.T_SelectionList.Add(objSelectList);
objprev.CreatedDate = DateTime.Now;
objprev.ModifiedDate = DateTime.Now;
objprev.Title = "Preview";
objprev.SelectionList_ID = objSelectList.ID;
objprev.PreviewStatus_ID = 2;
foreach (var im in itm)
{
objprev.OrderDetails_ID = im.orderid;
break;
}
context.T_Preview.Add(objprev);
foreach (var im in itm)
{
objAlbumDetails.CreatedDate = DateTime.Now;
objAlbumDetails.ModifiedDate = DateTime.Now;
objAlbumDetails.PhysicalPath = im.p_path;
objAlbumDetails.VirtualPath = im.v_path;
objAlbumDetails.Title = "Preview";
objAlbumDetails.UserAlbum_ID = objAlbum.ID;
objAlbumDetails.UploadedType = "Preview";
context.T_UserAlbumDetails.Add(objAlbumDetails);
objSelectListDetails.CreatedDate = DateTime.Now;
objSelectListDetails.ModifiedDate = DateTime.Now;
objSelectListDetails.SelectionType = "preview";
objSelectListDetails.Title = "preview";
objSelectListDetails.UserAlbumDetails_ID = objAlbumDetails.ID;
objSelectListDetails.SelectionList_ID = objSelectList.ID;
context.T_SelectionListDetails.Add(objSelectListDetails);
objprevdet.Title = "preview";
objprevdet.Preview_ID = objprev.ID;
objprevdet.SelectionListDetails_ID = objSelectListDetails.ID;
objprevdet.PreviewStatus_ID = 2;
context.T_PreviewDetails.Add(objprevdet);
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[T_PreviewDetails] OFF");
context.SaveChanges();
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[T_PreviewDetails] ON");
}
}