I'm new to Xamarin Forms, I am trying to get/Pass the Id value from XAML UI to my ViewModel
My XAML:
TODO
My VM:
private int id;
public int Id
{
get
{
return id;
}
set
{
if (id != value)
{
id = value;
OnPropertyChanged("Id");
}
}
}
public string result { get; set; }
public ICommand SubmitResultsCommand
{
get
{
return new Command(async () =>
{
IsLoading = true;
Result _result = new Result();
var response
= await _services.SubmitResultsAsync(result, id, Settings.AccessToken);
IsLoading = false;
});
}
}
in ViewModel
Define the binding property which you need to bind to view in xaml
public class MyViewModel: INotifyPropertyChanged
{
// it is necessary if you want to change the value of Id in runtime
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string id;
public string Id
{
get
{
return id;
}
set
{
if (id != value)
{
id = value;
NotifyPropertyChanged("Id");
}
}
}
// other properties
}
In ContentPage
Set the BindingContext
public MainPage()
{
InitializeComponent();
BindingContext = new MyViewModel();
}
I using WPF for my project. I have a want to get total value from Entities Framework Database and show it on main window. It work but when I changed value on datasource from sub window, it don't work automatically. I want after changed data on sub window, the total on main window will change immediately.
#namespace KhoLeco.ViewModel
{
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
// MainWindow:
namespace KhoLeco.ViewModel
{
public class MainViewModel : BaseViewModel
{
private double _TienDauCon;
public double TienDauCon { get => _TienDauCon; set { _TienDauCon = value; OnPropertyChanged(); } }
public bool IsLoaded = false;
public MainViewModel()
{
LoadedWindowCommand = new RelayCommand<Window>((p) => { return true; }, (p) =>
{
IsLoaded = true;
if (p == null)
return;
p.Hide();
LoginWindow loginWindow = new LoginWindow();
loginWindow.ShowDialog();
if (loginWindow.DataContext == null)
return;
var loginVM = loginWindow.DataContext as LoginViewModel;
if (loginVM.IsLogin)
{
p.Show();
LoadTienDau();
}
else
{
p.Close();
}
}
);
public void LoadTienDau()
{
TienDauCon =((int)DataProvider.Ins.DB.ChiTietPhieuDau.Sum(p => p.PayInfo) - (double)DataProvider.Ins.DB.ChiTietPhieuDau.Sum(p => p.Amount));
}
}
}
// TextBlock on main window:
<TextBlock Text="{Binding TienDauCon,IsAsync=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,StringFormat={}{0:#,#.00}}">
</TextBlock>
// sub window:
namespace KhoLeco.ViewModel
{
public class TienDauViewModel : BaseViewModel
{
private ObservableCollection<ChiTietPhieuDau> _List;
public ObservableCollection<ChiTietPhieuDau> List { get => _List; set
{ _List = value; OnPropertyChanged(); } }
private ObservableCollection<XeTai> _XeTai;
public ObservableCollection<XeTai> XeTai { get => _XeTai; set { _XeTai = value; OnPropertyChanged(); } }
private ObservableCollection<MatHangDau> _MatHangDau;
public ObservableCollection<MatHangDau> MatHangDau { get => _MatHangDau; set { _MatHangDau = value; OnPropertyChanged(); } }
private ChiTietPhieuDau _SelectedItem;
public ChiTietPhieuDau SelectedItem
{
get => _SelectedItem;
set
{
_SelectedItem = value;
OnPropertyChanged();
if (SelectedItem != null)
{
Id = SelectedItem.Id;
//DateSuply = SelectedItem.DateSuply;
SelectedXeTai = SelectedItem.XeTai;
SelectedMatHangDau = SelectedItem.MatHangDau;
Count = SelectedItem.Count;
Price = SelectedItem.Price;
Amount = SelectedItem.Amount;
DateBuy = SelectedItem.DateBuy;
CountLocate = SelectedItem.CountLocate;
MoreInfo = SelectedItem.MoreInfo;
PayInfo = SelectedItem.PayInfo;
}
}
}
private XeTai _SelectedXeTai;
public XeTai SelectedXeTai
{
get => _SelectedXeTai;
set
{
_SelectedXeTai = value;
OnPropertyChanged();
}
}
private MatHangDau _SelectedMatHangDau;
public MatHangDau SelectedMatHangDau
{
get => _SelectedMatHangDau;
set
{
_SelectedMatHangDau = value;
OnPropertyChanged();
}
}
private int _Id;
public int Id { get => _Id; set { _Id = value; OnPropertyChanged(); } }
private int _Count;
public int Count { get => _Count; set { _Count = value; OnPropertyChanged(); } }
private Nullable<double> _Price;
public Nullable<double> Price { get => _Price; set { _Price = value; OnPropertyChanged(); } }
private Nullable<double> _Amount;
public Nullable<double> Amount { get => _Amount; set { _Amount = value; OnPropertyChanged(); } }
private Nullable<DateTime> _DateBuy;
public Nullable<DateTime> DateBuy { get => _DateBuy; set { _DateBuy = value; OnPropertyChanged(); } }
private Nullable<DateTime> _DateSuply;
public Nullable<DateTime> DateSuply { get => _DateSuply; set { _DateSuply = value; OnPropertyChanged(); } }
private Nullable<double> _CountLocate;
public Nullable<double> CountLocate { get => _CountLocate; set { _CountLocate = value; OnPropertyChanged(); } }
private Nullable<int> _PayInfo;
public Nullable<int> PayInfo { get => _PayInfo; set { _PayInfo = value; OnPropertyChanged(); } }
private string _MoreInfo;
public string MoreInfo { get => _MoreInfo; set { _MoreInfo = value; OnPropertyChanged(); } }
public ICommand PhieuDauCommand { get; set; }
public ICommand EditCommand { get; set; }
public TienDauViewModel()
{
List = new ObservableCollection<ChiTietPhieuDau>(DataProvider.Ins.DB.ChiTietPhieuDau.OrderByDescending(x => x.Id));
XeTai = new ObservableCollection<XeTai>(DataProvider.Ins.DB.XeTai);
MatHangDau = new ObservableCollection<MatHangDau>(DataProvider.Ins.DB.MatHangDau);
PhieuDauCommand = new RelayCommand<object>((p) => { return true; }, (p) =>
{
PhieuDauWindow wd = new PhieuDauWindow();
wd.ShowDialog();
});
EditCommand = new RelayCommand<object>((p) =>
{
if (SelectedItem == null)
return false;
var displayList = DataProvider.Ins.DB.ChiTietPhieuDau.Where(x => x.Id == SelectedItem.Id);
if (displayList != null && displayList.Count() != 0)
return true;
return false;
}, (p) =>
{
var TienDau = DataProvider.Ins.DB.ChiTietPhieuDau.Where(x => x.Id == SelectedItem.Id).SingleOrDefault();
TienDau.Id = Id;
TienDau.IdTruck = SelectedXeTai.Id;
TienDau.IdObject = SelectedMatHangDau.Id;
TienDau.Count = Count;
TienDau.Price = Price;
TienDau.Amount = Count * Price;
TienDau.CountLocate = CountLocate;
TienDau.DateBuy = DateBuy;
TienDau.PayInfo = PayInfo;
TienDau.MoreInfo = MoreInfo;
DataProvider.Ins.DB.SaveChanges();
// May I call LoadTienDau() here?
});
}
}
}
I have a JSON class file which contains three classes, all of which follow this structure:
public class ManifestJSON : INotifyPropertyChanged
{
[JsonProperty("dataType")]
private string dataType;
public string DataType
{
get
{
return dataType;
}
set
{
if(dataType != value)
{
dataType = value;
RaisePropertyChanged("DataType");
}
}
}
[JsonProperty("ttl")]
private int time_to_live;
public int Time_To_Live
{
get
{
return time_to_live;
}
set
{
if (time_to_live != value)
{
time_to_live = value;
RaisePropertyChanged("Time_To_Live");
}
}
}
[JsonProperty("serial")]
private long serial;
public long Serial
{
get
{
return serial;
}
set
{
if (serial != value)
{
serial = value;
RaisePropertyChanged("Serial");
}
}
}
[JsonProperty("modifiedIso8601")]
private string modifiedIso8601;
public string ModifiedIso8601
{
get
{
return modifiedIso8601;
}
set
{
if (modifiedIso8601 != value)
{
modifiedIso8601 = value;
RaisePropertyChanged("ModifiedIso8601");
}
}
}
[JsonProperty("modifiedTimestamp")]
private long modifiedTimestamp;
public long ModifiedTimestamp
{
get
{
return modifiedTimestamp;
}
set
{
if (modifiedTimestamp != value)
{
modifiedTimestamp = value;
RaisePropertyChanged("ModifiedTimestamp");
}
}
}
[JsonProperty("timezone")]
private string timezone;
public string Timezone
{
get
{
return timezone;
}
set
{
if (timezone != value)
{
timezone = value;
RaisePropertyChanged("Timezone");
}
}
}
[JsonProperty("exports")]
private ObservableCollection<ManifestItem> manifest_Items;
public ObservableCollection<ManifestItem> Manifest_Items
{
get
{
return manifest_Items;
}
set
{
if (manifest_Items != value)
{
manifest_Items = value;
RaisePropertyChanged("Manifest_Items");
}
}
}
//Event handling
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
Console.WriteLine("Updated");
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
In another class, I've created a global instance of type ManifestJSON
public ManifestJSON manifestData;
which is filled by deserializing a JSON string into this object using the DeserializeObject method from the Newtonsoft.json library like so:
manifestData = JsonConvert.DeserializeObject<ManifestJSON>(JSONString).
This fills the ManifestJSON class successfully, but none of my property methods or events are triggering. What am I doing wrong here?
If you want to update your existing data-bound ManifestJSON object, you should not replace this one with a new object but de-serialize the JSON string into new object and then set the properties of the existing manifestData object:
var newData = JsonConvert.DeserializeObject<ManifestJSON>(JSONString);
manifestData.DataType = newData.DataType;
manifestData.Time_To_Live = newData.Time_To_Live;
manifestData.Serial = newData.Serial;
//...
I'm a complete C# novice, please excuse my ignorance.
I'm trying to parse string values into a view-model. I'm having difficulty converting the database DateTime and Boolean values into strings as part of the LineOne, LineTwo and LineThree properties. How do I do this?
private void mapChecks()
{
bool FoundResult = false;
// Check if object is loaded
if (Items.Count == 0)
{
//Add everything
foreach (xtn_UnresolvedCheck check in MyChecks)
{
Items.Add(new ItemViewModel
{
LineOne = check.ClientName,
LineTwo = check.NSMDateTime,
LineThree = check.HaveRead,
MyappId = check.MonitoringID
}
);
}
}
ItemViewModel:
namespace App
{
public class ItemViewModel : INotifyPropertyChanged
{
private int _myappId;
public int MyappId
{
get
{
return _myappId;
}
set
{
if (value != _myappId)
{
_myappId = value;
NotifyPropertyChanged("MyappId");
}
}
}
private bool _isFavorite;
public bool IsFavorite
{
get
{
return _isFavorite;
}
set
{
if (value != _isFavorite)
{
_isFavorite = value;
NotifyPropertyChanged("IsFavorite");
}
}
}
private string _lineOne;
public string LineOne
{
get
{
return _lineOne;
}
set
{
if (value != _lineOne)
{
_lineOne = value;
NotifyPropertyChanged("LineOne");
}
}
}
private string _lineTwo;
public string LineTwo
{
get
{
return _lineTwo;
}
set
{
if (value != _lineTwo)
{
_lineTwo = value;
NotifyPropertyChanged("LineTwo");
}
}
}
private string _lineThree;
public string LineThree
{
get
{
return _lineThree;
}
set
{
if (value != _lineThree)
{
_lineThree = value;
NotifyPropertyChanged("LineThree");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
You should do this
Items.Add( new ItemViewModel
{
LineOne = check.ClientName,
LineTwo = check.NSMDateTime.ToString(),
LineThree = check.HaveRead.ToString(),
MyappId = check.MonitoringID
});
Use ToString();
or cast to string
I am writing a boring application to manage patients and their clinic history. I used SQLite combined with DbLinq libraries and DbMetal code generation utility. Here are two classes from the genereated code extracted from the underlying database:
[Table(Name="main.Patients")]
public partial class Patient : System.ComponentModel.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged
{
private static System.ComponentModel.PropertyChangingEventArgs emptyChangingEventArgs = new System.ComponentModel.PropertyChangingEventArgs("");
private long _birthday;
private string _firstName;
private int _hasChildren;
private System.Nullable<int> _id;
private int _isMarried;
private string _lastName;
private string _profession;
private EntitySet<ClinicCase> _clinicCases;
private EntitySet<PatientAddress> _patientsAddresses;
private EntitySet<PatientPhoneNumber> _patientsPhoneNumbers;
#region Extensibility Method Declarations
partial void OnCreated();
partial void OnBirthdayChanged();
partial void OnBirthdayChanging(long value);
partial void OnFirstNameChanged();
partial void OnFirstNameChanging(string value);
partial void OnHasChildrenChanged();
partial void OnHasChildrenChanging(int value);
partial void OnIDChanged();
partial void OnIDChanging(System.Nullable<int> value);
partial void OnIsMarriedChanged();
partial void OnIsMarriedChanging(int value);
partial void OnLastNameChanged();
partial void OnLastNameChanging(string value);
partial void OnProfessionChanged();
partial void OnProfessionChanging(string value);
#endregion
public Patient()
{
_clinicCases = new EntitySet<ClinicCase>(new Action<ClinicCase>(this.ClinicCases_Attach), new Action<ClinicCase>(this.ClinicCases_Detach));
_patientsAddresses = new EntitySet<PatientAddress>(new Action<PatientAddress>(this.PatientsAddresses_Attach), new Action<PatientAddress>(this.PatientsAddresses_Detach));
_patientsPhoneNumbers = new EntitySet<PatientPhoneNumber>(new Action<PatientPhoneNumber>(this.PatientsPhoneNumbers_Attach), new Action<PatientPhoneNumber>(this.PatientsPhoneNumbers_Detach));
this.OnCreated();
}
[Column(Storage="_birthday", Name="Birthday", DbType="integer", AutoSync=AutoSync.Never, CanBeNull=false)]
[DebuggerNonUserCode()]
public long BirthdayBinaryDate
{
get
{
return this._birthday;
}
set
{
if ((_birthday != value))
{
this.OnBirthdayChanging(value);
this.SendPropertyChanging();
this._birthday = value;
this.SendPropertyChanged("Birthday");
this.OnBirthdayChanged();
}
}
}
[Column(Storage="_firstName", Name="FirstName", DbType="text", AutoSync=AutoSync.Never, CanBeNull=false)]
[DebuggerNonUserCode()]
public string FirstName
{
get
{
return this._firstName;
}
set
{
if (((_firstName == value)
== false))
{
this.OnFirstNameChanging(value);
this.SendPropertyChanging();
this._firstName = value;
this.SendPropertyChanged("FirstName");
this.OnFirstNameChanged();
}
}
}
[Column(Storage="_hasChildren", Name="HasChildren", DbType="integer", AutoSync=AutoSync.Never, CanBeNull=false)]
[DebuggerNonUserCode()]
public int HasChildren
{
get
{
return this._hasChildren;
}
set
{
if ((_hasChildren != value))
{
this.OnHasChildrenChanging(value);
this.SendPropertyChanging();
this._hasChildren = value;
this.SendPropertyChanged("HasChildren");
this.OnHasChildrenChanged();
}
}
}
[Column(Storage="_id", Name="ID", DbType="integer", IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
[DebuggerNonUserCode()]
public System.Nullable<int> ID
{
get
{
return this._id;
}
set
{
if ((_id != value))
{
this.OnIDChanging(value);
this.SendPropertyChanging();
this._id = value;
this.SendPropertyChanged("ID");
this.OnIDChanged();
}
}
}
[Column(Storage="_isMarried", Name="IsMarried", DbType="integer", AutoSync=AutoSync.Never, CanBeNull=false)]
[DebuggerNonUserCode()]
public int IsMarried
{
get
{
return this._isMarried;
}
set
{
if ((_isMarried != value))
{
this.OnIsMarriedChanging(value);
this.SendPropertyChanging();
this._isMarried = value;
this.SendPropertyChanged("IsMarried");
this.OnIsMarriedChanged();
}
}
}
[Column(Storage="_lastName", Name="LastName", DbType="text", AutoSync=AutoSync.Never, CanBeNull=false)]
[DebuggerNonUserCode()]
public string LastName
{
get
{
return this._lastName;
}
set
{
if (((_lastName == value)
== false))
{
this.OnLastNameChanging(value);
this.SendPropertyChanging();
this._lastName = value;
this.SendPropertyChanged("LastName");
this.OnLastNameChanged();
}
}
}
[Column(Storage="_profession", Name="Profession", DbType="text", AutoSync=AutoSync.Never)]
[DebuggerNonUserCode()]
public string Profession
{
get
{
return this._profession;
}
set
{
if (((_profession == value)
== false))
{
this.OnProfessionChanging(value);
this.SendPropertyChanging();
this._profession = value;
this.SendPropertyChanged("Profession");
this.OnProfessionChanged();
}
}
}
#region Children
[Association(Storage="_clinicCases", OtherKey="PatientID", ThisKey="ID", Name="fk_ClinicCases_0")]
[DebuggerNonUserCode()]
public EntitySet<ClinicCase> ClinicCases
{
get
{
return this._clinicCases;
}
set
{
this._clinicCases = value;
}
}
[Association(Storage="_patientsAddresses", OtherKey="PatientID", ThisKey="ID", Name="fk_PatientsAddresses_0")]
[DebuggerNonUserCode()]
public EntitySet<PatientAddress> Addresses
{
get
{
return this._patientsAddresses;
}
set
{
this._patientsAddresses = value;
}
}
[Association(Storage="_patientsPhoneNumbers", OtherKey="PatientID", ThisKey="ID", Name="fk_PatientsPhoneNumbers_0")]
[DebuggerNonUserCode()]
public EntitySet<PatientPhoneNumber> PhoneNumbers
{
get
{
return this._patientsPhoneNumbers;
}
set
{
this._patientsPhoneNumbers = value;
}
}
#endregion
public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging;
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
System.ComponentModel.PropertyChangingEventHandler h = this.PropertyChanging;
if ((h != null))
{
h(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(string propertyName)
{
System.ComponentModel.PropertyChangedEventHandler h = this.PropertyChanged;
if ((h != null))
{
h(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
#region Attachment handlers
private void ClinicCases_Attach(ClinicCase entity)
{
this.SendPropertyChanging();
entity.Patient = this;
}
private void ClinicCases_Detach(ClinicCase entity)
{
this.SendPropertyChanging();
entity.Patient = null;
}
private void PatientsAddresses_Attach(PatientAddress entity)
{
this.SendPropertyChanging();
entity.Patient = this;
}
private void PatientsAddresses_Detach(PatientAddress entity)
{
this.SendPropertyChanging();
entity.Patient = null;
}
private void PatientsPhoneNumbers_Attach(PatientPhoneNumber entity)
{
this.SendPropertyChanging();
entity.Patient = this;
}
private void PatientsPhoneNumbers_Detach(PatientPhoneNumber entity)
{
this.SendPropertyChanging();
entity.Patient = null;
}
#endregion
}
[Table(Name="main.PatientsAddresses")]
public partial class PatientAddress : System.ComponentModel.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged
{
private static System.ComponentModel.PropertyChangingEventArgs emptyChangingEventArgs = new System.ComponentModel.PropertyChangingEventArgs("");
private string _address;
private string _domicileStatus;
private System.Nullable<int> _patientID;
private EntityRef<Patient> _patients = new EntityRef<Patient>();
#region Extensibility Method Declarations
partial void OnCreated();
partial void OnAddressChanged();
partial void OnAddressChanging(string value);
partial void OnDomicileStatusChanged();
partial void OnDomicileStatusChanging(string value);
partial void OnPatientIDChanged();
partial void OnPatientIDChanging(System.Nullable<int> value);
#endregion
public PatientAddress()
{
this.OnCreated();
}
[Column(Storage="_address", Name="Address", DbType="text", IsPrimaryKey=true, AutoSync=AutoSync.Never)]
[DebuggerNonUserCode()]
public string Address
{
get
{
return this._address;
}
set
{
if (((_address == value)
== false))
{
this.OnAddressChanging(value);
this.SendPropertyChanging();
this._address = value;
this.SendPropertyChanged("Address");
this.OnAddressChanged();
}
}
}
[Column(Storage="_domicileStatus", Name="DomicileStatus", DbType="text", AutoSync=AutoSync.Never)]
[DebuggerNonUserCode()]
public string DomicileStatus
{
get
{
return this._domicileStatus;
}
set
{
if (((_domicileStatus == value)
== false))
{
this.OnDomicileStatusChanging(value);
this.SendPropertyChanging();
this._domicileStatus = value;
this.SendPropertyChanged("DomicileStatus");
this.OnDomicileStatusChanged();
}
}
}
[Column(Storage="_patientID", Name="PatientID", DbType="integer", IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.Never)]
[DebuggerNonUserCode()]
public System.Nullable<int> PatientID
{
get
{
return this._patientID;
}
set
{
if ((_patientID != value))
{
if (_patients.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnPatientIDChanging(value);
this.SendPropertyChanging();
this._patientID = value;
this.SendPropertyChanged("PatientID");
this.OnPatientIDChanged();
}
}
}
#region Parents
[Association(Storage="_patients", OtherKey="ID", ThisKey="PatientID", Name="fk_PatientsAddresses_0", IsForeignKey=true)]
[DebuggerNonUserCode()]
public Patient Patient
{
get
{
return this._patients.Entity;
}
set
{
if (((this._patients.Entity == value)
== false))
{
if ((this._patients.Entity != null))
{
Patient previousPatients = this._patients.Entity;
this._patients.Entity = null;
previousPatients.Addresses.Remove(this);
}
this._patients.Entity = value;
if ((value != null))
{
value.Addresses.Add(this);
_patientID = value.ID;
}
else
{
_patientID = null;
}
}
}
}
#endregion
public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging;
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
System.ComponentModel.PropertyChangingEventHandler h = this.PropertyChanging;
if ((h != null))
{
h(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(string propertyName)
{
System.ComponentModel.PropertyChangedEventHandler h = this.PropertyChanged;
if ((h != null))
{
h(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
I use the following code to add an address to a patient:
PatientAddress address = new PatientAddress();
address.Address = txtAddress.Text;
address.DomicileStatus = cmbDomicileStatus.Text;
currentPatient.Addresses.Add(address);
Database.Source.PatientsAddresses.InsertOnSubmit(address);
Database.Source.SubmitChanges();
Database.Source is an instance of the class that extends DataContext in the generated code. On SubmitChanges, I receive this exception:
"Equal operator is not defined between Nullable(Of Int32) and Int32."
The message is not reported word by word, but the meaning is the same. The stack trace point to DbLinq code, more precisely to line 709 of source file DbLinq.Data.Linq.DataContext.cs. You can find the source files here: http://dblinq.codeplex.com/SourceControl/changeset/view/16800#314775 (under the body of the method SetEntityRefQueries(object entity)).
I see that the problem comes when comparing a foreign key value with a constant in an expression tree, but I couln't manage to get other information on that. Can you help me find the issue?
N.B.: the field address.PatientID (foreign key) is actually set to the correct value before the invocation of SubmitChanges.
As I mentioned in the comment above (which I'm repeating here so I can link images), your primary key should not be nullable. There should be a property in your mapping that you can change to set it, although I don't use DbLinq, so I can't give you a screenshot of it directly. Instead, here it is in the LINQ-2-SQL DBML designer (left) and the Entity Framework EDMX designer (right).
I'm not as sure about your deletion problem - that seems like it should work to me. Can you edit your question to include the whole block of your deletion code? As a preliminary guess, you're either creating a new object (instead of loading one) and then trying to delete it, or you're deleting the association without deleting the object.
As a general rule, I never delete from a database when I can avoid it - I just mark inactive. It's much easier to "undelete" that way.
Did you try: address.Patient = currentPatient
instead of: currentPatient.Addresses.Add(address)?
PatientAddress address = new PatientAddress();
address.Address = txtAddress.Text;
address.DomicileStatus = cmbDomicileStatus.Text;
address.Patient = currentPatient;
Database.Source.PatientsAddresses.InsertOnSubmit(address);
Database.Source.SubmitChanges();