簡體   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