简体   繁体   中英

Entity Framework - adding additional properties to model

I'm new to the Entity Framework but I managed to create a database and several models.

Here's my Rider model:

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

I also have another partial class to include additional properties:

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

And this is my context class:

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; }
}

However, when initializing my context I receive this error:

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.'

Now, I do understand it's thinking that Dash.Game.Room is another field, but I don't want it to treat it like a field, but as a property I'm adding so the class can interact with my program. I want to include the columns inside my first class, and the additional properties in the other one.

How can I achieve this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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