lundi 17 novembre 2014

How to save information from one Model to Another?


Vote count:

0




I'm using entity framework 6 to access a database and manupulate it's tables and their values. Everything seems to work fine but there's something I can't figure out.


In my datasources I have objects representing tables in the database (MySQL). Naturally these are what are manipulated before changes are persisted to the database.


IN this scenario, I have two tables. Signup and user. There's a page where employees enter their credentials such as names, usernames, passwords, gender and date of birth. When they click on submit, this information goes into the Signup table waiting for the administrator to either reject them, or approve of them as valid employees.


So when Admin approves of the employee, he/she clicks on a button that copies the row into another table reserved for verified users, hence the table name 'users'. The functionality of the method meant to do that is what's above my knowledge.


Below is the xaml in the signup userControl the data context is set to xaml declared signup view source. The elements that are bound to this CollectionViewSource are textboxes that hold user credentials.



<Grid DataContext="{StaticResource signupsViewSource}">
<TextBox x:Name="idTextBox" Text="{Binding id, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="firstnameTextBox" Text="{Binding firstname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="surnameTextBox" Text="{Binding surname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="usernameTextBox" Text="{Binding username, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="passwordTextBox" Text="{Binding password, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>


Now in the App.xaml ResourceDictionary I declared the Collection view source so this collection view source was accessible to all UserControls that needed to make use of it.



<ResourceDictionary>
<CollectionViewSource x:Key="signupViewSource"/>
</ResourceDictionary>


Which now brings me to the ViewModel.



public class registrationVM
{
#region Declarations
// CollectionViewSource declaration set to find the one declared in the App.xaml ResourceDictionary
CollectionViewSource signupViewSource = (CollectionViewSource)Application.Current.Resources["signupViewSource"];
// Entity declaration
mascamainEntities context = new mascamainEntities();

#endregion

#region Constructor

public registrationSQL()
{
context.signups.Load();
signupViewSource.Source = context.signups.Local;
signupViewSource.View.MoveCurrentToFirst();
}

#endregion

#region Model properties
// signup is the table object I wish to save the information from
private signup _signup;

public signup Signup
{
get { return _signup; }
}

#endregion

// This bit of code is triggered by and ICommand property on button click
private object SaveCurrent_CommandExecute(object param)
{
try
{
// 'MyApp' is the solution, and 'user' is the table object I wish to save to
// The below code doesn't work though because I cannot expose the properties
// that the CollectionViewSource bind the UI Elements to.
// VS insists that I create a new instance of it.
MyApp.user User = new Medcare2.user
{
id = Signup.id,
first = Signup.first,
last = Signup.last,
username = Signup.username,
password = Signup.password,
question = Signup.question,
};

// Persist Changes to database
context.users.Add(User);
context.SaveChanges();
MessageBox.Show("Saved");
}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

return null;
}


As you may guess, the exception that's thrown is the classic object reference not set to an Instance of an object, which I can understand. What I don't know is a way to migrate the information to another table object (Model). Any help ideas?



asked 1 min ago

Offer

141






How to save information from one Model to Another?

Aucun commentaire:

Enregistrer un commentaire