LINQ to SQL -主/外键设置

本文关键字:设置 to SQL LINQ | 更新日期: 2023-09-27 18:15:04

我想把相册在我的应用程序和学习MVVM和LINQ和数据库交互在c#所有在同一时间。我对数据库有问题,特别是设置键。

我的错误是:

附加信息:无法在"相册"类型上找到密钥"PhotoID"的密钥成员"PhotoID"。键可能是错误的,或者'Album'上的字段或属性更改了名称。

我改变了一些东西,希望解决它,但没有工作。我认为我的问题源于对EntitySetEntityRef不够了解。

编辑:

[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();
        }
    }

LINQ to SQL -主/外键设置

在您的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/en-us/library/bb386950 (v = vs.110) . aspx

http://msdn.microsoft.com/en-us/library/bb386951 (v = vs.110) . aspx