I'm trying to put a photo album in my app AND learn MVVM and LINQ and database interaction in C# all at the same time. I'm having issues with the DB, specifically, setting up the keys.
My error is this:
Additional information: Could not find key member 'PhotoID' of key 'PhotoID' on type 'Album'. The key may be wrong or the field or property on 'Album' has changed names.
I changed some things around hoping to fix it, but that did not work. My problem stems from not understanding EntitySet
and EntityRef
well enough, I believe.
Edit:
[Table]
public class Album : BaseModel
{
// Version column aids update performance.
[Column(IsVersion = true)]
private Binary _version;
private int _albumID;
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int AlbumID
{
get { return _albumID; }
set
{
if (_albumID != value)
{
NotifyPropertyChanging();
_albumID = value;
NotifyPropertyChanged();
}
}
}
private EntitySet<Photo> _photos;
//Proxy Class used to get pickuplines related to the category
//OtherKey = ForeignKey
//ThisKey = PrimaryKey
[Association(Storage = "_photos", OtherKey = "AlbumID", DeleteRule = "CASCADE", ThisKey = "PhotoID")]
public EntitySet<Photo> Photos
{
get { return _photos; }
set
{
this._photos.Assign(value);
}
}
[Table]
public class Photo : BaseModel
{
// Version column aids update performance.
[Column(IsVersion = true)]
private Binary _version;
private int _photoID;
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int PhotoID
{
get { return _photoID; }
set
{
if (_photoID != value)
{
NotifyPropertyChanging();
_photoID = value;
NotifyPropertyChanged();
}
}
}
[Column]
internal int _albumID;
private EntityRef<Album> _album;
// ForeignKey
// Proxy Class for the PrimaryKey Class
// ThisKey = ForeignKey
// PtherKey = PrimaryKey
[Association(Storage = "_album", ThisKey = "AlbumID", DeleteRule = "CASCADE", OtherKey = "PhotoID", IsForeignKey = true)]
public Album Album
{
get { return _album.Entity; }
set
{
NotifyPropertyChanging();
_album.Entity = value;
if (value != null)
{
_albumID = value.AlbumID;
}
NotifyPropertyChanged();
}
}
In your Album
association, you specify the AlbumID
foreign key, but no such property exists in your entity. You have to expose a public property of it in your Photo
entity.
private int _albumID;
[Column(CanBeNull = false)]
public int AlbumID
{
get { return _albumID; }
set
{
if (_albumID != value)
{
NotifyPropertyChanging();
_albumID = value;
NotifyPropertyChanged();
}
}
}
More on associations:
http://msdn.microsoft.com/en-us/library/bb386950(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/bb386951(v=vs.110).aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.