我正在使用现有数据库的实体框架。我在该数据库中更改了一个表的列,当我尝试将记录插入该关系时出现此错误:
... IDENTITY_INSERT设置为OFF
实际上,身份设置为ON。我想这个问题是对表进行的更新。由于没有创建模型,我如何通知EF有关此更新的信息。 Ť 这是我的控制器
[HttpPost]
public JsonResult SaveReview( Review review, HttpPostedFileBase image ) {
string userID = Membership.GetUser().ProviderUserKey.ToString();
review.UserID = userID;
review.Date = DateTime.Now;
reviewRepository.SaveReview( review );
}
public class EFReviewRepository : IReviewRepository{
private EFDbContext context = new EFDbContext();
public IQueryable<Review> Reviews {
get {
return context.Reviews;
}
}
public void SaveReview( Review review ) {
context.Reviews.Add( review );
context.SaveChanges();
}
}
ReviewID int //auto-increment field
UserID nvarchar(50)
HotelID int
Title nvarchar(100)
[Content] text
Date date
Rating int
最后,评论模型是:
/**A Hotel Review entity*/
public class Review {
[ScaffoldColumn( false )]
public int ReviewID { get; set; }
[ScaffoldColumn( false )]
public string UserID { get; set; }
[ScaffoldColumn( false )]
[Key, ForeignKey( "Hotel" ), Column(Order = 1)]
public int HotelID { get; set; }
public virtual Hotel Hotel { get; set; }
[Required( ErrorMessage = "Please enter a Title" )]
public string Title { get; set; }
[Required( ErrorMessage = "Please enter a description" )]
[DataType( DataType.MultilineText )]
public string Content { get; set; }
public DateTime Date { get; set; }
[Required( ErrorMessage = "Please enter a rating" )]
public int Rating { get; set; }
}