实体框架 - 向模型添加其他属性

时间:2017-04-09 05:40:31

标签: c# entity-framework

我是实体框架的新手,但我设法创建了一个数据库和几个模型。

这是我的Rider模型:

namespace Dash.Data
{
    public partial class Rider
    {
        public int ID { get; set; }
        public int AccountID { get; set; }
        public string Name { get; set; }
    }
}

我还有另一个部分类来包含其他属性:

namespace Dash.Data
{
    public partial class Rider
    {
        public Room Room { get; set; }
    }
}

这是我的上下文类:

public class DashContext : DbContext
{
    public DashContext()
        : base(Constants.ConnectionString)
    {
        this.Configuration.LazyLoadingEnabled = true;
        this.Configuration.ProxyCreationEnabled = false;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer<DashContext>(null);
    }

    public DbSet<Rider> Riders { get; set; }
}

但是,在初始化我的上下文时,我收到此错误:

System.InvalidOperationException: 'Unable to determine the principal end of an association between the types 'Dash.Game.Room' and 'Dash.Data.Rider'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.'

现在,我确实理解它认为Dash.Game.Room是另一个字段,但我不希望它像对待字段一样对待它,而是作为我正在添加的属性,所以类可以与我的程序交互。我希望在第一个类中包含列,在另一个类中包含其他属性。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

public partial class Rider
{
    [NotMapped]
    public Room Room { get; set; }
}
相关问题