0

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?

2
  • You must register Room as a complex type if it contains properties that are mapped to database fields. It's not clear from your question if it does. Commented Apr 9, 2017 at 8:21
  • By the way, you won't get lazy loading if ProxyCreationEnabled is disabled. Commented Apr 9, 2017 at 8:25

1 Answer 1

1
public partial class Rider
{
    [NotMapped]
    public Room Room { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.