开发者

Update ONLY some fields of attached Entity

I'm using .NET 3.5 SP1. I have entity 'AppUser':

public class AppUser : System.Data.Objects.DataClasses.EntityObject{   
 public int Uid {get; set;}   
 public string UserName {get; set;}   
 public string Password {get; set;}   
 public DateTime LastLogin {get; set;}   
 public string Name {get; set;}   
 public string Address {get; set;}
 public string Comment {get; set;} 
 ...........   
} 

To update ALL fields of an attached enitity:

   public void Update(AppUser updateUser) {
    AppUser user = ctx.AppUserSet.Where(u => u.UserId == userId).FirstOrDefault();
    //This will update ALL fields
    ctx.ApplyPropertyChanges(user.EntityKey.EntitySetName, updateUser);
    ctx.SaveChanges();   
    }  

I want to update all fields except Password and LastLogin. I can update individual fields, but w开发者_运维百科ill be cumbersome for entities with large number of fields.

Please tell, what is the best way to achive this?

Thank You.


Change the setters for those two properties to private in the EDMX/model.


I don't think that you can do this directly with EF without extra programming effort.

There are three possibilities:

  • Update through a view
  • Update using a stored procedure
  • Change your data model, such that the password and lastlogin are in a seperate table with a one to one relationship.

All of these require extra programming effort.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜