[英]LINQ to SQL and adding multiple records to foreign table for a single primary key
[英]LINQ to SQL - Primary/Foreign key setup
我试图将相册放入我的应用程序中,并同时在C#中学习MVVM和LINQ以及数据库交互。 我在数据库方面遇到问题,特别是在设置密钥方面。
我的错误是这样的:
附加信息:在类型“相册”上找不到键“ 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();
}
}
在“ Album
关联中,指定“ AlbumID
外键,但是您的实体中不存在此类属性。 您必须在Photo
实体中公开它的公共属性。
private int _albumID;
[Column(CanBeNull = false)]
public int AlbumID
{
get { return _albumID; }
set
{
if (_albumID != value)
{
NotifyPropertyChanging();
_albumID = value;
NotifyPropertyChanged();
}
}
}
有关关联的更多信息:
http://msdn.microsoft.com/zh-cn/library/bb386950(v=vs.110).aspx
http://msdn.microsoft.com/zh-cn/library/bb386951(v=vs.110).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.