调用SaveChanges()后为什么实体未在数据库中更改? [英] Why entity is not changed in database after calling SaveChanges()?

查看:76
本文介绍了调用SaveChanges()后为什么实体未在数据库中更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从EF上下文中获取实体,将其传输到客户端,更改DateTime属性和EntityProperty(引用另一个实体)。在此之后,我将实体发送到服务器并应用更改。之后,我收到只有DateTime属性应用于数据库,但未应用引用属性。


 //客户端
locCustomer .ModifiedDate = DateTime.Now;
locCustomer.SalesTerritory = newSalesTerritory;

//服务器端
object obj;
if(context.TryGetObjectByKey(v.EntityKey,out obj))
{
context.ApplyPropertyChanges(v.EntityKey.EntitySetName,v);
}
var res = context.SaveChanges();


我做错了什么?使用AdventureWorks架构,如何从上下文获取"客户"对象,分离它,创建mannualy新的"SalesTerritory"对象,将新的"SalesTerritory"对象设置为分离的"Customer"对象的属性"SalesTerritory",并将所有这些对象保存到EF上下文。这听起来很简单,但在EF中是一个挑战。当所有对象都附加到上下文时它很简单,但是如何使用分离的对象呢?是否有可能找到工作示例?

解决方案

我必须为其他表(密钥约束)的引用创建单独的实体对象。以下是我在使用EF创建的视频播放器应用程序中使用的一些代码的示例。这可能不是最好的方式,但这是我唯一可以开展的工作。我希望这会有所帮助:




使用 ctx As EFEntities = EFEntities()
试试 Dim oVideo =( 来自 v ctx.VideoSet _
其中 v.VideoID = gVideoID _
选择 v).First

sVideoName = oVideo.Title





Dim oUser =( 来自 u ctx.UserSet _
< span style ="color:#0000ff; font-size:x-small"> 其中 u.UserID = gUserID _
选择 u).First



Dim oNewReview As 审核
oNewReview.ReviewID = Guid.NewGuid()
oNewR eview.Users = oUser
oNewReview.Videos = oVideo
oNewReview.DateCreated = Now()
oNewReview.ReviewText = Request.QueryString(
" txt"

ctx.AddToReviewSet(oNewReview)
ctx.SaveChanges()





Dim oNewRating 作为 评级
oNewRating.RatingID = Guid.NewGuid()
oNewRating.Users = oUser
oNewRating.Videos = oVideo
oNewRating。 Rate =
CInt (Request.QueryString( " rate" )))o newRating.DateCreated =现在()

ctx.AddToRatingSet(oNewRating)
ctx.SaveChanges()





Dim oVideoRatingAvg =( 来自 r ctx.RatingSet _
加入 v ctx.VideoSet 开启 r.Videos.VideoID 等于 < span style ="font-size:x-small"> v.VideoID _
其中 r.Videos.VideoID = gVideoID _
选择 < span style ="font-size:x-small"> VAvg = r.Rate)。平均值oV ideo.Rating = oVideoRatingAvg.Value

ctx.SaveChanges()



sData + ="一堆HTML"







Catch ex 作为 例外

sData + ="



保存审核时出错。请重试。"





结束 试用 结束 使用


I get entity from the EF-context, transmit it to the client, change DateTime property and EntityProperty (reference to another Entity). After this I send entity to the server and apply changes. And after that I receive that only DateTime property is applied to the database, but reference property is not applied.

//Client
locCustomer.ModifiedDate = DateTime.Now;
locCustomer.SalesTerritory = newSalesTerritory;

//Server Side
object obj;
if (context.TryGetObjectByKey(v.EntityKey, out obj))
{
     context.ApplyPropertyChanges(v.EntityKey.EntitySetName, v);
}
var res = context.SaveChanges();

What I do wrong? Using AdventureWorks schema, how can I get 'Customer' object from context, detach it, create mannualy new 'SalesTerritory' object, set new 'SalesTerritory' object to the property 'SalesTerritory' of detached 'Customer' object and save all these into the EF context. It sounds simple, but is is a challenge in the EF. When all objects are attached to the context it is simple, but how can I do it with detached objects? Is it possible to find working example?

解决方案

I've had to create separate entity objects for the references to other tables  (key constrained).  Here is an example of some code I used in a video player application that I created with EF. It might not be the best way, but that's the only thing that I could get to work. I hope this helps:

 

Using ctx As EFEntities = New EFEntities()
Try
Dim oVideo = (From v In ctx.VideoSet _
Where v.VideoID = gVideoID _
Select v).First

sVideoName = oVideo.Title

 

Dim oUser = (From u In ctx.UserSet _
Where u.UserID = gUserID _
Select u).First

 

Dim oNewReview As New Review
oNewReview.ReviewID = Guid.NewGuid()
oNewReview.Users = oUser
oNewReview.Videos = oVideo
oNewReview.DateCreated = Now()
oNewReview.ReviewText = Request.QueryString(
"txt")

ctx.AddToReviewSet(oNewReview)
ctx.SaveChanges()

 

Dim oNewRating As New Rating
oNewRating.RatingID = Guid.NewGuid()
oNewRating.Users = oUser
oNewRating.Videos = oVideo
oNewRating.Rate =
CInt(Request.QueryString("rate"))
oNewRating.DateCreated = Now()

ctx.AddToRatingSet(oNewRating)
ctx.SaveChanges()

 

Dim oVideoRatingAvg = (From r In ctx.RatingSet _
Join v In ctx.VideoSet On r.Videos.VideoID Equals v.VideoID _
Where r.Videos.VideoID = gVideoID _
Select VAvg = r.Rate).Average
oVideo.Rating = oVideoRatingAvg.Value

ctx.SaveChanges()

sData += "A bunch of HTML"

 

Catch ex As Exception

sData += "

Error saving review. Please try again."

 

End Try
End Using


这篇关于调用SaveChanges()后为什么实体未在数据库中更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆