CTP5。不支持设置IsModified = false [英] CTP5. Set IsModified = false is not Supported

查看:108
本文介绍了CTP5。不支持设置IsModified = false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

entry.Property(" Password")。IsModified = false;


------------------- ----------------------


执行此操作后:entry.CurrentValues.SetValues(obj);


我想还原属性("密码")


的更改但我不能手动设置


-----------------


所以,我现在有2个解决方案


1)在entry.CurrentValues.SetValues(obj)之后设置动作"entry.CurrentValues.SetValues(obj);"


2)之前的obj值; [;密码""由本


变种OriginalPass = entry.OriginalValues设置OBJ值到原来的值。的ToString();

ori.Password = OriginalPass ;


entry.Property(" Password")。IsModified = false; //我不想更新它但我不能设置为false


 


-------------------------------- -----


 


 

解决方案

< blockquote>

Sipo,


 


允许将IsModified设置为false是我们计划在未来支持的事情但它需要对核心EF组件进行更改,因此它在第一版Code First / Productivity Improv的
中无法使用ements。 


 


就目前而言,正如您所说,您最好的办法是确保密码永远不会被标记为已修改。 
需要注意的一点是,如果修改后的对象上的Password值与数据库中对象的值相同,则调用SetValues不会将其标记为已修改。&NBSP;
例如,这应该有效:


 


               
var entity = context.Foos.Find(obj.Id);


        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
obj.Password = entity.Password;


               
c​​ontext.Entry(实体).CurrentValues.SetValues(OBJ);


<跨度风格= "font-family:Consolas; font-size:9.5pt"> 


另一种方法是明确只为您感兴趣的属性设置新值。 
例如:


 


               
foreach var propertyName
in context.Entry(entity)。CurrentValues.PropertyNames)


<跨度风格="">&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
{


<跨度风格="">&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
如果(propertyName!=
"密码"


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
{


<跨度风格="">&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;
context.Entry(entity).Property(propertyName)。CurrentValue =


               &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
context.Entry(obj).Property(propertyName)。CurrentValue;


<跨度风格= "">&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
}


               
}


 


您还可以创建一个DTO类型对象,该对象仅包含您感兴趣的属性,而是设置该对象的当前值。 
例如:


 


   
public class
Foo


   
{


       
public int Id {
获得; set ; }


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public string 用户名{
获得; set ; }


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public string 密码{
获得; set ; }


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public string Blah {
获得; set ; }


&NBSP;&NBSP;&NBSP;
}


 


&NBSP;&NBSP;&NBSP;
public class
FooDto


   
{


       
public int Id {
获得; set ; }


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public string 用户名{
获得; set ; }


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public string Blah {
获得; set ; }


&NBSP;&NBSP;&NBSP;
}


 


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
...


 


< span style ="">               
var obj = new
FooDto (); //填充所有DTO属性


 


               
var entity = context.Foos.Find(obj.Id);


        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
c​​ontext.Entry(实体).CurrentValues.SetValues(OBJ);


<跨度风格= "font-family:Consolas; font-size:9.5pt"> 


希望这有帮助。


 


谢谢,


Arthur


entry.Property("Password").IsModified = false;

-----------------------------------------

After I do this : entry.CurrentValues.SetValues(obj);

I want to revert changes for Property("Password")

but I can not set by hand

-----------------

so, there is 2 solution for me now

1) set the obj value before the action "entry.CurrentValues.SetValues(obj);"

2) after entry.CurrentValues.SetValues(obj); set the obj value to original value by this

var OriginalPass = entry.OriginalValues["Password"].ToString();
ori.Password = OriginalPass;

entry.Property("Password").IsModified = false;// I don't want to update it but I can't set to false

 

-------------------------------------

 

 

解决方案

Sipo,

 

Allowing setting IsModified to false is something we plan to support in the future but it requires changes to the core EF assembly so it won’t be available in the first release of Code First/Productivity Improvements. 

 

For now, as you say, your best bet is to make sure that Password never gets marked as modified.  One thing to note is that if the value of Password on the modified object is the same as it is on the object from the database, then calling SetValues will not mark it as modified.  For example, this should work:

 

                var entity = context.Foos.Find(obj.Id);

                obj.Password = entity.Password;

                context.Entry(entity).CurrentValues.SetValues(obj);

 

Another approach is to explicitly only set new values for the properties that you are interested in.  For example:

 

                foreach (var propertyName in context.Entry(entity).CurrentValues.PropertyNames)

                {

                    if (propertyName != "Password")

                    {

                        context.Entry(entity).Property(propertyName).CurrentValue =

                            context.Entry(obj).Property(propertyName).CurrentValue;

                    }

                }

 

You could also create a DTO type object that only contains the properties you are interested in and instead set current values from that object.  For example:

 

    public class Foo

    {

        public int Id { get; set; }

        public string Username { get; set; }

        public string Password { get; set; }

        public string Blah { get; set; }

    }

 

    public class FooDto

    {

        public int Id { get; set; }

        public string Username { get; set; }

        public string Blah { get; set; }

    }

 

       ...

 

                var obj = new FooDto(); // Populate all DTO properties

 

                var entity = context.Foos.Find(obj.Id);

                context.Entry(entity).CurrentValues.SetValues(obj);

 

Hope this helps.

 

Thanks,

Arthur


这篇关于CTP5。不支持设置IsModified = false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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