如何将属性映射到列

时间:2014-07-29 17:42:12

标签: c# linq entity-framework

就像我在question中提到的那样,我想重命名一些自动生成的属性。

所以我使用以下的部分类

    public partial class Plan
    {
        public Profile Creator
        {
            get { return this.Profile; }
            set
            {
                this.Profile = value;
            }
        }

        public Profile Guest
        {
            get { return this.Profile1; }
            set
            {
                this.Profile1 = value;
            }
        }
    }

避免使用Profile1Profile。它可以工作,但是我不能在where子句中使用这些新属性,因为它们没有被映射(这是我的猜测)。

示例:

myQuery.Where(x => x.Creator.User.UserName == userName)

我有以下异常

  

指定的类型成员'创作者' LINQ不支持   实体。仅初始化程序,实体成员和实体导航   支持属性。

我试图像这样映射属性但没有成功

[Column("creator_id", TypeName="int")]
public Profile Creator
{
    get { return this.Profile; }
    set
    {
        this.Profile = value;
    }
}

有可能吗?

1 个答案:

答案 0 :(得分:0)

我认为你正在寻找像这样的东西

  [ForeignKey("creator_id")]
  public virtual Profile Creator { get; set; }

这些通常用于代码优先方法的东西。如果您有现有数据库,则可以首先对代码进行反向工程。查看http://msdn.microsoft.com/en-us/data/jj200620.aspx以获取示例。这样您就可以使用流畅的API工具包和现有数据库。两个世界中最好的。