【问题标题】:LINQ to SQL - Primary/Foreign key setupLINQ to SQL - 主键/外键设置
【发布时间】:2014-07-28 03:16:07
【问题描述】:

我正在尝试将相册放入我的应用程序并同时学习 MVVM 和 LINQ 以及 C# 中的数据库交互。我遇到了数据库问题,特别是设置密钥。

我的错误是这样的:

附加信息:在类型“相册”上找不到键“PhotoID”的键成员“PhotoID”。键可能有误,或者“相册”上的字段或属性已更改名称。

我改变了一些东西,希望能修复它,但这并没有奏效。我的问题源于对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();
        }
    }

【问题讨论】:

标签: c# database linq windows-phone-8 mvvm


【解决方案1】:

在您的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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多