I am building a Windows Phone application and am facing a database processing problem. Part of the requested database (approximately) looks like this: two tables USER {ID} - the identifier is the primary key ACCOUNT {ID, OWNERID, BALANCE} - ID and OWNERID is the composite primary key, which means that the user account ID is unique only in within the user's area.
It was assumed that the composite primary key in the last table should be used, since the account must be user-defined, and there is a fairly high probability that several users define accounts with the same identity.
So in C # it looks like this:
[Table]
public class User
{
[Column(IsVersion = true)]
private Binary _version;
public User()
{
_accountsRef = new EntitySet<Account>(OnAccountAdded, OnAccountRemoved);
}
private string _id;
[Column(IsPrimaryKey = true, IsDbGenerated = false, CanBeNull = false, Storage = "_id")]
public string ID {
get { return _id; }
set
{
if(string.Equals(value, ID, StringComparison.InvariantCultureIgnoreCase))
return;
RaisePropertyChanging("ID");
_id = value;
RaisePropertyChanged("ID");
}
}
private EntitySet<Account> _accountsRef;
public EntitySet<Account> Accounts
{
get { return _accountsRef; }
}
private void OnAccountAdded(Account account)
{
account.Owner = this;
}
private void OnAccountRemoved(Account account)
{
account.Owner = null;
}
....
}
[Table]
public class Account : PropertyChangingNotificator
{
[Column(IsVersion = true)]
private Binary _version;
private string _ownerId;
[Column(IsPrimaryKey = true, CanBeNull = false)]
public string OWNER_ID
{
get { return _ownerId; }
set
{
if (value == OWNER_ID)
return;
RaisePropertyChanging("OWNER_ID");
_ownerId = value;
RaisePropertyChanged("OWNER_ID");
}
}
private EntityRef<User> _ownerRef;
[Association(Name = "FK_USER_ACCOUNTS", ThisKey = "OWNER_ID", OtherKey = "ID", Storage = "_ownerRef", IsForeignKey = true)]
public User Owner {
get { return _ownerRef.Entity; }
set
{
User previousValue = _ownerRef.Entity;
if(previousValue==value)
{
return;
}
if(previousValue!=null)
{
_ownerRef.Entity = null;
previousValue.Accounts.Remove(this);
}
RaisePropertyChanging("Owner");
_ownerRef.Entity = value;
if(value!=null)
{
value.Accounts.Add(this);
OWNER_ID = value.ID;
}
else
{
OWNER_ID = string.Empty;
}
RaisePropertyChanged("Owner");
}
}
}
Here is a query that asks for user accounts with an id:
var accounts = from a in dataContext.Accounts
where a.OWNER_ID == userName
select a;
List<Account> accoutsList = accounts.ToList();
As a result, I get the correct list of accounts, each element of which has a corresponding User object as its Owner property.
But in case I try to get the user object from the datacontext as follows:
var user = (from u in dataContext.Users
where u.ID.ToUpper() == userName.ToUpper()
select u).
SingleOrDefault();
property user.Accounts has EntitySet without loaded values (also even after call user.Accounts.Load();)
In the debug buffer, the account has the following property values:
HasAssignedValues == false;
HasLoadedOrAssignedValues == false;
HasLoadedValues == false;
HasSource == false;
HasValues == true;
IsDeffered == false;
IsLoaded == false;
- : Mango Windows Phone (SQL CE): [Association]
Accounts [Association (Name = "FK_USER_ACCOUNTS" )], datacontext , FK_USER_ACCOUNTS.
thisKey = " ID", OtherKey = " OWNER_ID" - , , (ACCOUNT.ID ACCOUNT.OWNERID) , ( ACCOUNT.ID). - , .
- , ?
.
.