0

I am trying to create a ModelBuilder within my API and can't seem to add .HasRequired() to my code. I am assuming this is due to the fact that it lives within DBModelBuilder, however, I cannot add that also.

It will only allow me to use ModelBuilder. Otherwise it throws an error: OnModelCreating(DbModelBuilder): no suitable method found to override

Am I missing something here?

My DbContext looks like so:

public class TicketContext : DbContext
    {
        public DbSet<Tickets> Tickets { get; set; }
        public DbSet<Users> Users { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder) // <-- Not allowing me to add DbModelBuilder here
        {
            modelBuilder.Entity<Tickets>().HasRequired(t => t.Users);

        }
    }

Update

I have (as a test) started a new project from scratch. I created a new ASP.NET 5.0 project using a Web API template. Straight away my project doesn't recognize DbModelBuilder. I added reference to EntityFramework.dll and still no good. I then added using System.Data.Entity; and it then accepted DbModelBuilder but still complains that the namespace 'DbModelBuilder' could not be found.

I can't understand how I can have this error upfront on a brand new project?

As soon as I try and add the package Entity Framework from NuGet, I get more errors that version 6.1.3 is not compatible with DNX Core 5.0

I can't seem to find any examples/solutions to any of these errors.

Update 2

I have managed to get DbModelBuilder recognized now by adding the EntityFramework.dll reference to the DNX Core 5.0 Assembly as well as the DNX 4.5.1 assembly, however, now it has thrown even more errors wanting System.Core added and mscorlib. I really can't believe how much trouble it is to create a (what I thought would be simple) Web API project.

Riples
  • 1,167
  • 2
  • 21
  • 54
  • I'm having a similar problem, it just keeps getting this error that OnModelCreating has no override. I don't understand why. It's worked just fine before but now it won't. – Nathan McKaskle Jan 09 '18 at 20:35
  • I'm not sure sorry. I gave up with this in the end. Seemed too much trouble for such a simple exercise. – Riples Jan 13 '18 at 01:18

1 Answers1

0

You can't just call HasRequired off the model builder - you need to indicate the entity you want to change:

public class MyContext: DbContext 
{ 
    public DbSet<Foo> Foos { get; set; } 
    public DbSet<Bar> Bars { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
        modelBuilder.Entity<Foo>().HasRequired(f => f.Bar);   // assumes Bar navigation added to Foo
    } 
} 

https://msdn.microsoft.com/en-us/data/jj591617.aspx?f=255&MSPPError=-2147217396

What I like to do is separate out my fluent code like this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
       modelBuilder.Configurations.Add(new FooConfig());
    } 

Now I can create a class with all my mapping for that entity:

public class FooConfig : EntityTypeConfiguration<Foo>
{
    public FooConfig()
    {
        // Primary Key
        this.HasKey(t => t.FooId);

        // Relationships
        this.HasRequired(f => f.Bar).WithMany(b => b.Foos);

    }
}
Steve Greene
  • 12,029
  • 1
  • 33
  • 54
  • Sorry, I should've been more specific. I have updated my post to show my current code that isn't working. – Riples Feb 17 '16 at 22:59
  • 1
    The thing is that it doesn't recognise `DbModelBuilder` only `ModelBuilder`, so I can't add `.HasRequired()`. – Riples Feb 17 '16 at 23:06
  • If you use System.Data.Entity.DbModelBuilder does it work? – Steve Greene Feb 17 '16 at 23:07
  • No, I get the error: `The type or namespace name 'Entity' does not exist in the namespace 'System.Data'` – Riples Feb 17 '16 at 23:09
  • What's your EF version? – Steve Greene Feb 17 '16 at 23:09
  • I'm assuming it's `EntityFramework.Core": "7.0.0-rc1-final"`. If not, could you please tell me where I can find this for you. Thanks – Riples Feb 17 '16 at 23:12
  • Got to run out, but looks like you may be missing a reference – Steve Greene Feb 17 '16 at 23:14
  • I would also like to separate out my fluent code, but I cannot add the DbModelBuilder as an parameter to my method without adding the EnittyFramework reference to my project containing the Fluent API code, and adding just the System.Data.Entity dll did not work. Please have a look at my question here:http://stackoverflow.com/questions/36141356/cannot-use-dbmodelbuilder-in-seperate-project – monstertjie_za Mar 21 '16 at 21:08
  • In the latest release of Microsoft.EntityFrameworkCore, the OnModelCreating override accepts a ModelBuilder and not a DbModelBuilder, so when you attempt to use `builder.Entity().HasRequired`, it's missing. Try the latest release and you'll see. There is not .HasRequired – clockwiseq Jul 31 '16 at 03:05