繁体   English   中英

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

[英]Entity Framework - adding additional properties to model

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

这是我的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是另一个字段,但是我不希望它将其像一个字段一样对待,但是我要添加一个属性,以便类可以与我的程序进行交互。 我想将列包括在我的第一堂课中,并将其他属性包括在另一堂课中。

我该如何实现?

public partial class Rider
{
    [NotMapped]
    public Room Room { get; set; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM