New Posts New Posts RSS Feed: SaveChangesAsync(IEnumerable)
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

SaveChangesAsync(IEnumerable)

 Post Reply Post Reply
Author
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Topic: SaveChangesAsync(IEnumerable)
    Posted: 27-Jul-2010 at 4:06pm

Glad you were able to get that working.

We do not have any current audit trail examples, and depending on your requirements, there are a number of implementation choices.  One thing you should take a look at is the EntityServerSaveInterceptor.  There you can inspect and modify the entities and add auditing information, or even create new entities that can be added to the transaction.
 
Back to Top
Vincent View Drop Down
Newbie
Newbie


Joined: 21-May-2010
Location: Tanzania
Posts: 22
Post Options Post Options   Quote Vincent Quote  Post ReplyReply Direct Link To This Post Posted: 27-Jul-2010 at 8:54am
Hi ting
 
Thank you very much indeed. Through your help, I have managed to remove entities from both cache and the datagrid.
 
Do you have any sample function or application that manages audit trails in Devforce?
 
Best Regards,
Vincent
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 20-Jul-2010 at 7:41pm
Hi Vincent,
 
Do you want to remove entities from showing up in the DataGrid or from the EntityManager's cache?  If you want to remove it from the grid, then you should remove it from the ItemsSource list that provides the data for the grid.
 
You can remove it from the EntityManager cache using RemoveEntities (as you do above), and the Entity will be in a detached state until all references to it are gone at which point it should get garbage collected.  I really wouldn't be concerned about removing it from cache unless you have hundreds of thousands of entities floating around, though.
 
Back to Top
Vincent View Drop Down
Newbie
Newbie


Joined: 21-May-2010
Location: Tanzania
Posts: 22
Post Options Post Options   Quote Vincent Quote  Post ReplyReply Direct Link To This Post Posted: 17-Jul-2010 at 8:47am
Hi ting
 
I have been trying to remove entities from my entity manager without success and there is no any error given out. I have a data grid whereby I add new entities. I want to have a flexibility to remove unwanted entities. Here is my code
 

private void buttonRemove_Click(object sender, RoutedEventArgs e)

{

List<owner> EntityRemove = new List<owner>();

owner _owner = (owner)MyDataGrid.SelectedItem;

EntityRemove.Add(_owner);

mgr.RemoveEntities(EntityRemove);

}

Could you please help me here.

 
Best Regards,
Vincent
Back to Top
Vincent View Drop Down
Newbie
Newbie


Joined: 21-May-2010
Location: Tanzania
Posts: 22
Post Options Post Options   Quote Vincent Quote  Post ReplyReply Direct Link To This Post Posted: 15-Jul-2010 at 11:24pm
ting and Kimj
 
I truly appreciate your help.
 
I managed to add a reference of my class on the silverlight client side as Kimj instructed me and I used

#if !SILVERLIGHT

#endif
for those assemblies not supported by silverlight and now everything is working with my custom login manager.
 
Thank you very much indeed gentle men.
 
Best Regards,
Vincent
Back to Top
Vincent View Drop Down
Newbie
Newbie


Joined: 21-May-2010
Location: Tanzania
Posts: 22
Post Options Post Options   Quote Vincent Quote  Post ReplyReply Direct Link To This Post Posted: 15-Jul-2010 at 10:03pm

Thank you very much Kimj

The purpose of choosing to use custom login is to enable me to extend to the existing database which has user particulars and unfortunately the tables there are not set up according to ASP.Net authentication needs. So, in the MyUserBase class, I have added a link to the existing database and it requires the use of assemblies related to data access.

Unfortunately silverlight does not support use of data access assemblies like System.Data. How can I add a reference to client silverlight without running to trouble of those assemblies and yet be able to access my existing database?
 
 
Best Regards,
Vincent
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 15-Jul-2010 at 7:55pm
The MyUserIdentity and MyUserBase classes must also be defined on both client and server.  The error seems to indicate that the Silverlight application doesn't know about the MyUserBase class.   You've probably defined these custom classes in the web application project, but you also need to link the files containing these classes into your Silverlight application project.
 
Also, there's no need to define custom types here, unless you do later plan to add your own properties or logic.  You can use UserBase and UserIdentity as is if they meet your needs.


Edited by kimj - 15-Jul-2010 at 7:57pm
Back to Top
Vincent View Drop Down
Newbie
Newbie


Joined: 21-May-2010
Location: Tanzania
Posts: 22
Post Options Post Options   Quote Vincent Quote  Post ReplyReply Direct Link To This Post Posted: 15-Jul-2010 at 7:10pm
Thank you ting
 
I have tried to implement user identity as shown below but I am still getting the same error.
 
 
 
  UserIdentity identity = new MyUserIdentity(mUserName, "Custom", true);
          
  UserBase userbs = new MyUserbase(identity, GetRoles(mUserName));
           
   return userbs;
 
 
 
 
The UserIdentity implementation is as follows
 
    [DataContract]
    [DiscoverableType(DiscoverableTypeMode.KnownType)]
    public class MyUserIdentity : UserIdentity
    {
        public MyUserIdentity(string Name, string type, bool isAuthenticated)
            : base(Name, type, isAuthenticated)
        {
          
        }
    }
 
 
Best regards,
Vincent
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 15-Jul-2010 at 6:20pm

Instead of using AppIdentity, use UserIdentity.  We are updating our samples to reflect this.

Back to Top
Vincent View Drop Down
Newbie
Newbie


Joined: 21-May-2010
Location: Tanzania
Posts: 22
Post Options Post Options   Quote Vincent Quote  Post ReplyReply Direct Link To This Post Posted: 15-Jul-2010 at 12:56am

Hi ting

 

I tried to follow the steps to told me but still I am getting an error as show below.

 

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://idealblade.com/EntityModel:LoginResult. The InnerException message was ‘Element ’ http://idealblade.com/EntityModel:Principal’ contains data of the ‘http://schema.datacontract.org/2004/07/DevCombobox:MyUserbase’ data contact. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to ‘MyUserbase’ to the list of known types for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer. Please see InnerException for more details.

 

 

 

 

Here is my  implementation of IEntityLoginManager

 

 

public class LoginManager : IEntityLoginManager

    {

      

        [DebuggerNonUserCode]

        public IPrincipal Login(ILoginCredential pCredential, IdeaBlade.EntityModel.EntityManager pEntityManager)

        {

          

         string mUserName = string.Empty;

         string mPassword = string.Empty;

 

         if (pCredential == null)

         {

             mUserName = "admin";

             mPassword = "12345678";

         }

         else

         {

             mUserName = pCredential.UserName;

             mPassword = pCredential.Password;

 

         }

                

         testEntities mManager = new testEntities(pEntityManager);

          

         VerifyCredentials(mUserName, mPassword);

            Int64 user_id = 1;

           

 

            IIdentity identity = new AppIdentity(mUserName, user_id, mPassword);

 

         

            UserBase userbs = new MyUserbase(identity, GetRoles(mUserName));

          

            return userbs;

 

        }

 

 

 

Here is my implementation of UserBase

 

 

 

    [DataContract]

    [DiscoverableType(DiscoverableTypeMode.KnownType)] 

    public class MyUserbase : UserBase

    {

 

       

        public MyUserbase(IIdentity identity, IList<string> roles)

            : base(identity, roles)

        {

          

        }

 

    }

 

 

Best Regards

Vincent.

Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 09-Jul-2010 at 5:07pm
Hi Vincent,
 
GenericPrincipal is not a native type in Silverlight so that's why it is having serialziation problems.  You can return a DevForce UserBase, or any IPrincipal that can be serialized.  To make a type serializable, you should
  1. Have the class "implement" the IKnownType interface.  (This is a marker interface, so no actual implementation is required.)
  2. Mark the class with the [DataContract] attribute
  3. Mark each serializable property with the [DataMember] attribute.

 

Back to Top
Vincent View Drop Down
Newbie
Newbie


Joined: 21-May-2010
Location: Tanzania
Posts: 22
Post Options Post Options   Quote Vincent Quote  Post ReplyReply Direct Link To This Post Posted: 08-Jul-2010 at 9:19pm
Hi ting
 
Thank you very much indeed. It has worked and I have learned where I was making mistake.
 
Would you please bear with me as I have something else to ask you.
 
I have been trying to implement my own custom IEntityLoginManager but I am continously getting serialization error. Here are the functions that invoke the class
 
IIdentity identity = new AppIdentity(mUserName, user_id, mPassword);

principal = new MyGenericPrincipal(identity, GetRoles(mUserName));

 
Below is my class implementing IPrincipal.

[DataContract]

[DiscoverableType(DiscoverableTypeMode.KnownType)]

public class MyGenericPrincipal : IPrincipal

{

private string[] _roles = null;

private IIdentity _identity = null;

public MyGenericPrincipal(IIdentity identity, string[] roles)

{

_roles = roles;

_identity = identity;

}

[DataMember]

public IIdentity Identity

{

get

{

return _identity;

}

set

{

_identity = value;

}

}

public bool IsInRole(string role)

{

bool found = true;

return found;

}

 

}

 
Thank you ting
 
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 08-Jul-2010 at 7:44pm
Hi Vincent,
 
I started with the DevForce Silverlight Template, mapped a few Entities from NorthwindIB, and added the code below.  It runs with no problems, and our unit tests are passing as well.
 
  public partial class MainPage : UserControl {
    public MainPage() {
      InitializeComponent();
      this.Loaded +=new RoutedEventHandler(MainPage_Loaded);
    }
 
    void MainPage_Loaded(object sender, RoutedEventArgs e) {
      var entityManager = new NorthwindIBEntities();
 
      Employee newEmployee = entityManager.CreateEntity<Employee>();
      newEmployee.FirstName = "Richard";
      newEmployee.LastName = "Feynman";
      newEmployee.EntityAspect.AddToManager();
 
      List<Employee> list = new List<Employee>();
      list.Add(newEmployee);
 
      var op = entityManager.SaveChangesAsync(list);
      op.Completed += new EventHandler<EntitySavedEventArgs>(op_Completed);
    }
 
    void op_Completed(object sender, EntitySavedEventArgs e) {
      if (e.HasError) MessageBox.Show("Save Failed: " + e.Exception.Message);
      else MessageBox.Show("Save Successful");
    }
  }
 
Back to Top
Vincent View Drop Down
Newbie
Newbie


Joined: 21-May-2010
Location: Tanzania
Posts: 22
Post Options Post Options   Quote Vincent Quote  Post ReplyReply Direct Link To This Post Posted: 07-Jul-2010 at 9:24pm

Thank you ting.

In fact I did not show the upper part of my code but It is there as shown below

              myObj bill = null;
              bill = manager.CreateEntity<myObj>();             
              bill.EntryDate = DateTime.Now;
              bill.ID = "Admin";

              bill.EntityAspect.AddToManager();

If I use SaveChangesAsync() it works fine. But If I use SaveChangesAsync(IEnumerable) it does not return any error and it does not insert the record into the database.
 
Could you please give me a sample of a working SaveChangesAsync(IEnumerable)
 
Thank you
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 07-Jul-2010 at 8:09pm
After you create a new entity you need to add it to the manager using EntityManager.AddEntity().
 
Back to Top
Vincent View Drop Down
Newbie
Newbie


Joined: 21-May-2010
Location: Tanzania
Posts: 22
Post Options Post Options   Quote Vincent Quote  Post ReplyReply Direct Link To This Post Posted: 07-Jul-2010 at 5:56am
Hi everybody
 
Can anyone help me here. I am trying to use SaveChangesAsync(IEnumerable) to persist my entities into the database and every time I click save everything goes without error, but there is no entity inserted into the database. If I use SaveChangesAsync(), everything goes successifully. Here is the code I am using
 

if (EM.HasChanges())

{

ClassArray = new MyEntities[1];

ClassArray[0] = new MyEntities("Entity1");

TestClass test = new TestClass("Entity1");

//var op = mgr.SaveChangesAsync(test);

var op = EM.SaveChangesAsync(ClassArray);

op.Completed += (s, args) =>{
if (null == args.Error){}else{MessageBox.Show(args.Error.Message, "Error", MessageBoxButton.OKCancel);}

};

}

 
 
Below, are the classes MyEntities and TestClass. I tried to use each of the class below but non has worked.
 
 
 [DataContract]

public class MyEntities

{

[DataMember]

public string _SavedEntities;

public MyEntities()

{

}

public MyEntities(string SavedEntities)

{

this._SavedEntities = SavedEntities;

}

}

class TestClass : System.Collections.IEnumerable

{

public string _Entity = string.Empty;

public TestClass(string entity)

{

_Entity = entity;

}

public System.Collections.IEnumerator GetEnumerator()

{

yield return _Entity;

}

}

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down