【发布时间】:2014-07-28 03:16:07
【问题描述】:
我正在尝试将相册放入我的应用程序并同时学习 MVVM 和 LINQ 以及 C# 中的数据库交互。我遇到了数据库问题,特别是设置密钥。
我的错误是这样的:
附加信息:在类型“相册”上找不到键“PhotoID”的键成员“PhotoID”。键可能有误,或者“相册”上的字段或属性已更改名称。
我改变了一些东西,希望能修复它,但这并没有奏效。我的问题源于对EntitySet 和EntityRef 不够了解,我相信。
编辑:
[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();
}
}
【问题讨论】:
-
请把相关代码贴在这里。
-
这是实体框架吗?
-
这是基于此的 Windows Phone 开发:msdn.microsoft.com/en-us/library/windows/apps/…LINQ to SQL?
标签: c# database linq windows-phone-8 mvvm